I have a weird situation. I have 2 objects: state.result which is an array of objects and data.catalogs which is an array of objects similar to state.result.
I want to create a pure function to return copy of both of them merged (in a redux reducer). If I run:
{
...state,
result: {...state.result, ...data.catalogs}
}
Everything is hunkey dorey and the 4 items in state.result are merged with the 1 item in data.catalogs leaving me with a return value of 4 items. The problem is I want an array of items and this gives me and object with 4 properties containg the items.
If I run:
{
...state,
result: [...state.result, ...data.catalogs]
}
It gives me an array of items like I want but instead of merging them, it appends them so I get an array of 5 items (a duplicate of the item in state.result and data.catalogs).
Why is this happening and how can I merge items within an array?
state.resultcontains onlydata.catalogs? Show how your state looks like ( the relevant part )