0

我有以下函数来获取 currentStatus 和 id 并根据上述参数更改值。我有以下状态。

this.state.column : [
  [
    arr1 : { id: 1, currentStatus: ''to-do'}, 
           { id: 4, currentStatus: ''completed'},
  ],
  [
    arr2 : { id: 10, currentStatus: ''in-progress'}, 
           { id: 14, currentStatus: ''completed'},
  ],
]

我将新的 currentStatus 和特定的 id 作为参数提供给以下函数,并且需要用新的 currentStatus 替换现有的 currentStatus。特定对象可以通过 id 找到(对于整个数组来说是唯一的)。我正在尝试以下代码,最后得到错误Cannot read property 'nn' of undefined

import update from 'immutability-helper';

getCurrentStatus = (currentStatus, id) => {
    Object.keys(this.state.columns).forEach(ee => {
      if (this.state.columns[ee].find(x => x.id === id)){
        var nn = this.state.columns[ee].findIndex(x => x.id === id)
        this.setState({
          columns : update(this.state.columns, {ee:{nn:{$set : currentStatus}}})
        }, () =>
          console.log(this.state.columns)
        )
      }
    })
  }
4

1 回答 1

3

在您的代码eenn变量中是键和索引,所以我假设您希望您使用这些变量中的值编写一个对象。所以,你的update电话应该是这样的:

update(this.state.columns, { [ee]: { [nn]: { $set : currentStatus }}})

我们使用括号来定义带有变量的键,如下所示:

const key = 'test';
console.log({ [key]: true }); // print: { test: true }
于 2019-04-23T11:38:31.323 回答