简体   繁体   English

在React.js中附加我的状态数组

[英]Appending my state array in React.js

clickHandler(type) {



    for (var i = 0; i < this.state.pokemon.length ; i++) {

            //console.log(this.state.pokemon[i]);
            //Do something temp1.types[0].type.name
            for(var j = 0; j < this.state.pokemon[i].types.length; j++) {

                if(this.state.pokemon[i].types[j].type.name == type.target.value) {
                    var toBeAppened = this.state.filteredPokemon.slice()
                    toBeAppened.push(this.state.pokemon[i])
                    this.setState({
                        filteredPokemon: this.state.filteredPokemon

                    })


                }


            }

    }
    console.log(this.state.filteredPokemon) 
}

This function is a handler of appending a specific part of data of my state array "pokemon" to another state array called "filteredPokemon". 此函数是将状态数组“ pokemon”的数据的特定部分附加到另一个名为“ filteredPokemon”的状态数组的处理程序。

After I run this, my filteredPokemon is just an empty array. 运行此命令后,我的filteredPokemon只是一个空数组。 I checked that this is valid by debugging with console.log, but it just does not append properly into my "filteredPokemon" 我通过用console.log调试检查了它是否有效,但是它没有正确地附加到我的“ filteredPokemon”中

What could be the problem? 可能是什么问题呢?

I could make an assumption that, the problem is, that you are setting wrong variable into state. 我可以假设问题是您将错误的变量设置为状态。 You should set toBeAppended array instead of 您应该将toBeAppended数组而不是

clickHandler(type) {
  for (var i = 0; i < this.state.pokemon.length; i++) {
    //console.log(this.state.pokemon[i]);
    //Do something temp1.types[0].type.name
    for (var j = 0; j < this.state.pokemon[i].types.length; j++) {
      if (this.state.pokemon[i].types[j].type.name == type.target.value) {
        var toBeAppened = this.state.filteredPokemon.slice();
        toBeAppened.push(this.state.pokemon[i]);
        // you are adding an item to toBeAppened array, therefore you should set this array into state
        this.setState({
          filteredPokemon: toBeAppened
        });
      }
    }
  }
  console.log(this.state.filteredPokemon);
}

The second issue, which could be, is that types array of every pokemon is empty, therefore the second nested loop is not invoking. 第二个问题可能是每个口袋妖怪的types数组都是空的,因此第二个嵌套循环没有调用。

Try to debug your if condition as well, probably this.state.pokemon[i].types[j].type.name == type.target.value expression is returning false 也尝试调试if条件,可能this.state.pokemon[i].types[j].type.name == type.target.value表达式返回false

I'm not sure how are you handling your data. 我不确定您如何处理数据。 what's the pokemon object like? 宠物小精灵对象是什么样的? To me the easiest way to handle your data and filter your pokemons should be something like this: 对我来说,处理数据和过滤口袋妖怪的最简单方法应该是这样的:

pokemon : {
   name: 'Charmander',
   type: 'Fire',
   (...)
}

That way filtering your array of pokemons should be pretty straightforward: 这样,过滤您的口袋妖怪阵列应该非常简单:

clickHandler(type) {
    const { pokemon } = this.state;
    const filteredPokemon = [];

    pokemon.forEach((_pokemon) => {
      if (_pokemon.type === type) {
        filteredPokemon.push(_pokemon);
      }
    });

    this.setState({ filteredPokemon });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM