'NGRX Reducer Set array to Empty inside in the Object
I have Store object structure as follows
{
"fooObject":[
{
"id":1,
"somefield":"bla bla",
"orderArray":[
{
"id":1,
"somefield":"a"
},
{
"id":2,
"somefield":"b"
},
{
"id":3,
"somefield":"c"
}
]
}
]
}
I need to make the orderArray empty on the NGRX reducer. and only need to change that array. I attempted to following to obtain my desired behavior but was unsuccessful.I tried using the Map method as well, but it failed.
on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
return {
...state,
fooObject: [
...state.productionLinesDisplayDetails,
{
orderArray: action.scheduledOrders
}
]
}
}),
Solution 1:[1]
The reducer will have frozen all the objects in the state tree so you'll have to create a new array with a new copy of the data.
on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
return {
...state,
fooObject: state.fooObject.map(obj => ({
...obj, // copy all other old values in each fooObject
orderArray: [] // replace the order-array with new empty array.
}))
}
}),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikkel Christensen |
