在React JS中更新对象数组的属性

时间:2018-12-17 11:50:36

标签: javascript reactjs redux react-redux

我是react-redux的新手。

我这里有一个类似的物体

const initialState = {
Low: [
    {
        id: 0,
        technologyId: 0,
        technology: '',
        type: '',
        count: '',
        allowded: 6,
        level: 'EASY'
    }
],
Medium: [
    {
        id: 0,
        technologyId: 0,
        technology: '',
        type: '',
        count: '',
        allowded: 7,
        level: 'MEDIUM'
    }
],
High: [
    {
        id: 0,
        technologyId: 0,
        technology: '',
        type: '',
        count: '',
        allowded: 7,
        level: 'TOUGH'
    }
]

}

现在,在reducer中将此值设置为props

现在,onchnage的对象属性已从这些obj之一更改。

所以,这里我更新它的方式是,

onChange(event, tobeupdated, id, type, noc, data) {
    let newData = { ...this.props.data };  
    if (newData) {
      let data = newData[type].map((object, index) => {
        if (object.id === id) {
          object[tobeupdated] = event.target.value;
      });
}

}

那么,我将在这里更新现有对象吗? 还是还有其他方法? 我尝试过的是

{...object, [tobeupdated]: event.target.value}

它给出了编译时错误。 我该如何解决?

0 个答案:

没有答案
相关问题