我很难弄清楚使用不可变助手修改对象数组中每个值的最佳实践。
例如,如果我的状态(state)是这样的:
this.state = {
items: [
{
name: "test",
subItems: [
{val: false}, {val: true}, {val: false}
]
},
{
name: "test2",
subItems: [
{val: true}, {val: true}, {val: false}
]
}
]
}
我希望将每个val
设置为false
,我该如何做到这一点。
我可以逐个完成,但肯定有更好的方法:
let elements = update(this.state.items, {
[idx1]:{
subItems:{
[idx2]:{
val: {
$set: false
}
}
}
}
});