Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否从react native中的列表中获取唯一项?_Javascript_Arrays_Reactjs_React Native_Jsx - Fatal编程技术网

Javascript 是否从react native中的列表中获取唯一项?

Javascript 是否从react native中的列表中获取唯一项?,javascript,arrays,reactjs,react-native,jsx,Javascript,Arrays,Reactjs,React Native,Jsx,以下是我的清单: newdiscoverPlanet: [ require('../img/sunp.png'), require('../img/twopp.png'), require('../img/bluep.png'), require('../img/purplep.png'), require('../img/bluepurplep.png'), require('../img/redp.png'), require('../img/orangep.pn

以下是我的清单:

newdiscoverPlanet: [
  require('../img/sunp.png'),
  require('../img/twopp.png'),
  require('../img/bluep.png'),
  require('../img/purplep.png'),
  require('../img/bluepurplep.png'),
  require('../img/redp.png'),
  require('../img/orangep.png')

],
我将所有这些都放在一个函数中,然后在这里使用
Math

getRandomPlanet = () =>{
  var planetItem = this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
  this.setState({
    changePlanet: planetItem,
  });
}
然后我将它们放在选项卡中,从列表中获得唯一的图像:

    _renderTabIndicator() {
       let tabs = [{
               text: `${this.state.tags.toLowerCase()}`,
               iconSource: `${this.state.changePlanet}`
           },{
               text: `${this.state.tags2.toLowerCase()}`,
               iconSource: `${this.state.changePlanet}`
           },{
               text: `${this.state.tags3.toLowerCase()}`,
               iconSource: `${this.state.changePlanet}`
       }];
       return <PagerTabIndicator tabs={tabs} />;
   }
\u renderTabIndicator(){
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
}];
返回;
}
但每次我加载页面时,我都会从每个源获得相同的图像。有没有办法让它们独一无二?如何在React Native中执行类似操作:
问题在于,您访问相同的值(this.state.changePlanet)3次,并期望得到不同的结果。 我想你只需要让方法返回一个随机行星,如下所示:

getRandomPlanet = () => {
  return this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
}
然后您可以调用此按钮3次,您将获得3个不同的图像:

_renderTabIndicator() {
       let tabs = [{
               text: `${this.state.tags.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
           },{
               text: `${this.state.tags2.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
           },{
               text: `${this.state.tags3.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
       }];
       return <PagerTabIndicator tabs={tabs} />;
   }
然后打电话:

_renderTabIndicator() {
  const planets = this.getRandomPlanets(3);
  let tabs = [{
                   text: `${this.state.tags.toLowerCase()}`,
                   iconSource: planets[0]
               },{
                   text: `${this.state.tags2.toLowerCase()}`,
                   iconSource: planets[1]
               },{
                   text: `${this.state.tags3.toLowerCase()}`,
                   iconSource: planets[2]
           }];
           return <PagerTabIndicator tabs={tabs} />;
       }
\u renderTabIndicator(){
const planets=this.getRandomPlanets(3);
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:行星[0]
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:行星[1]
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:行星[2]
}];
返回;
}

问题在于,您访问相同的值(this.state.changePlanet)3次,并期望得到不同的结果。 我想你只需要让方法返回一个随机行星,如下所示:

getRandomPlanet = () => {
  return this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
}
然后您可以调用此按钮3次,您将获得3个不同的图像:

_renderTabIndicator() {
       let tabs = [{
               text: `${this.state.tags.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
           },{
               text: `${this.state.tags2.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
           },{
               text: `${this.state.tags3.toLowerCase()}`,
               iconSource: this.getRandomPlanet()
       }];
       return <PagerTabIndicator tabs={tabs} />;
   }
然后打电话:

_renderTabIndicator() {
  const planets = this.getRandomPlanets(3);
  let tabs = [{
                   text: `${this.state.tags.toLowerCase()}`,
                   iconSource: planets[0]
               },{
                   text: `${this.state.tags2.toLowerCase()}`,
                   iconSource: planets[1]
               },{
                   text: `${this.state.tags3.toLowerCase()}`,
                   iconSource: planets[2]
           }];
           return <PagerTabIndicator tabs={tabs} />;
       }
\u renderTabIndicator(){
const planets=this.getRandomPlanets(3);
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:行星[0]
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:行星[1]
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:行星[2]
}];
返回;
}

这有点让人困惑。。您是否希望
this.state.changePlanet
每个选项卡都有不同的值?@azium yes i amwell这就像说
var a=1
然后希望
a
不等于
1
。您没有设置3个不同的值,而是设置一个值并使用该值3次。您不需要任何状态,只需调用
this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)]
其中
iconSource
是<代码>iconSource:请注意,这将获得不同的值,而不一定是唯一的。但是您应该从这里开始,但是在不同的iconSource中,图像是否有可能彼此相同?这有点令人困惑。。您是否希望
this.state.changePlanet
每个选项卡都有不同的值?@azium yes i amwell这就像说
var a=1
然后希望
a
不等于
1
。您没有设置3个不同的值,而是设置一个值并使用该值3次。您不需要任何状态,只需调用
this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)]
其中
iconSource
是<代码>iconSource:请注意,这将获得不同的值,而不一定是唯一的。但是您应该从这里开始,但是在不同的iconSource中,图像是否有可能彼此相同?