简体   繁体   English

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

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

How do you update a certain nested property in redux state? 如何在Redux状态下更新某个嵌套属性?

Let's say I only want to update the "value" property in the object below. 假设我只想更新下面对象中的“ value”属性。 I know you shouldn't deep copy the previous state but how do i only change the property of an object in an array in an object of an array? 我知道您不应该深深复制以前的状态,但是如何仅更改数组对象中数组对象的属性?

Thanks in advance! 提前致谢!

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

Something like the following: 类似于以下内容:

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

Something like this would work. 这样的事情会起作用。 (code looks ugly, didn't test though) (代码看起来很丑,虽然没有测试)

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