繁体   English   中英

更新嵌套数组中的属性值(redux状态)

[英]Update property value in nested array (redux state)

如何在Redux状态下更新某个嵌套属性?

假设我只想更新下面对象中的“ value”属性。 我知道您不应该深深复制以前的状态,但是如何仅更改数组对象中数组对象的属性?

提前致谢!

 market { shops: [ { name: 'abc', items: [ { name: 'item1', value: 40, id: '234rfds32' }, {} ] }, {}, {} ] } 

类似于以下内容:

 state = { ...state, shops: [ ...state.shops, shops[index].items = [ ...shops[index].items, ] ] }; 

这样的事情会起作用。 (代码看起来很丑,虽然没有测试)

var shop =  state.shops[index];
var items = [...shop.items];
items[<index>].value = 'your value';
shop.items = items;
var shops = [...state.shops];
shops[index] = shop;

state = {
...state, 
shops 
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM