I have two arrays of objects like this:
const oldArr = [
{ 'Tomorrow': [{ id: 2 }, { id: 4 }, { id: 3 }] },
{ 'Saturday': [{ id: 2 }, { id: 4 }, { id: 3 }] }
]
const newArr = [
{ 'Monday': [{ id: 2 },{ id: 4},{ id: 3 }] },
{ 'Tomorrow': [{ id: 1 },{ id: 5 }]}
]
I want to merge both without any duplicate keys, so it should result in:
[
{ 'Tomorrow': [{ id: 2 }, { id: 4 }, { id: 3 }, { id: 1 }, { id: 5 }] },
{ 'Saturday': [{ id: 2 }, { id: 4 }, { id: 3 }] },
{ 'Monday': [{ id: 2 }, { id: 4 }, { id: 3 }] }
]
As you can see, the contents of Tomorrow
have been added to the original Tomorrow
, and the object of Monday
has been added on.
I've vaguely figured out how to do so with nested loops, but I'm guessing there is a simpler solution using map
, reduce
, or the like.