我有一个看起来像这样的对象:
state: {
"1": {
"show": false,
"description": "one",
"children": {
"1": { "show": false, "description": "one" },
"2": { "show": false, "description": "one" }
}
},
"2": {
"show": false,
"description": "one",
"children": {
"1": { "show": false, "description": "one" },
"2": { "show": false, "description": "one" }
}
}
}
我有一个 for 循环,可以将孩子的“显示”属性更改为相反的布尔值。所以我尝试用这个更新值,但没有奏效。
for (var childKey in state[appClassId].children) {
newState = {
...state,
[appClassId]: {
children: {
[childKey]: { ...state[appClassId].children[childKey], show: !state[appClassId].children[childKey].show}
}
}
“appClassId”变量是我从操作中获得的变量。
如何更新子属性中的每个键,例如 state.1.children.1.show