Can we allow dynamic index of arrays to be update/add via a spread operator just like we do for objects, e.g here a reducer example:
const state = { myObj: {items: ["a", "b", "c"]} };
const index = 2;
const newValue = "d";
return {
...state,
myObj: {
...state.myObj,
items: [
...state.myObj.items,
[index]: newValue, // do something like this
]
}
} // => { myObj: {items: ["a", "b", "d"]} };
This has been requested at least 3 times on Stackoverflow, so its seems like natural pattern people want to use.
[1] References
- How to insert an item into an array at a specific index (JavaScript) - Stack Overflow
- javascript - How to update a specific index of an array when using spread operator - Stack Overflow
- javascript - How to update an element inside an array when using spread operator - Stack Overflow
Thanks