简体   繁体   中英

Update property value in nested array (redux state)

How do you update a certain nested property in redux state?

Let's say I only want to update the "value" property in the object below. 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 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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