【发布时间】:2017-06-26 11:57:14
【问题描述】:
我想改变一个类似这样的对象的属性,这是一个简化的对象,具有原始的一些属性:
state = {
pivotComuns: [
{
id: 1,
enabled : true
},
{
id: 2,
enabled : true
}
],
otherProperties : "otherProperties"
}
我正在像这样更改启用状态:
state = {
...state,
pivotColumns: {
...state.pivotColumns,
[2]: {
...state.pivotColumns[2], enabled: !state.pivotColumns[2].enabled
}
}
}
它可以工作,但不是像我一样返回一个数组,而是 pivotComuns 属性,它返回一个对象,“注意我将 [] 更改为 {}”:
state = {
pivotComuns: {
{
id: 1
enabled : true
},
{
id: 2,
enabled : true
}
},
otherProperties : "otherProperties"
}
我做错了什么,我需要将该属性保留为数组。
【问题讨论】:
-
您的原始代码缺少
{... -
@Aaron 不仅是
{,在对象数据中也缺少, -
对,我已经更新了代码。
标签: javascript reactjs redux react-redux spread-syntax