Update property value in nested array (redux state)
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,
]
]
;
market.shops[0].items[0].value = 10
@AnkitAgarwal Thanks for the fast reply but no, i updated the post to make it more clear what I'm looking for. I want to update the state with only the "value" property changed.
– stockholm-bound
Aug 22 at 9:20
1 Answer
1
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
;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
market.shops[0].items[0].value = 10
is this what you are looking for?– Ankit Agarwal
Aug 22 at 9:10