简体   繁体   English

更新嵌套数组中的数组而不会发生突变

[英]update array in nested array without mutation

Below code doesn't look good to me, as I have to declare a temp variable, any shorter code to achieve the same result? 下面的代码对我来说并不好,因为我必须声明一个temp变量,是否有任何较短的代码可以达到相同的结果?

handleChange = (e, index1, innerIndex) => {
  const temp_values = this.state.values
    temp_values.map((value, index) => {
      if (index === innerIndex) {
        temp_values[index].args[index1] = e.target.value
      }
    })
    this.setState({
      values: temp_values
    })
}

Yes, you can simplify it like this: 是的,您可以这样简化它:

handleChange = (e, index1, innerIndex) => {
  this.setState({
    values: this.state.values.map((value, index) => {
      const args = [].concat(value.args);
      if (index === innerIndex) {
        args[index1] = e.target.value;
      }

      return {
        ...value,
        args,
      };
    }),
  });
}

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

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