This question already has an answer here:
I'm trying combine multiple arrays by a specific key
property. For example, I have
arr1
[{
key: 'A',
items: [{name: 'a item'}]
}, {
key: 'B',
items: [{name: 'b item'}]
}]
arr2
[{
key: 'B',
items: [{name: 'another b item'}, {name: 'more b items'}, {name: 'even more b items'}]
}]
How do I produce the following arr3?
[{
key: 'A',
items: [{name: 'a item'}]
},
{
key: 'B',
items: [{name: 'b item'}, {name: 'another b item'}, {name: 'more b items'}, {name: 'even more b items'}]
}]
May use a hash table like this:
Some explanation:
Snippet I have that I've used many times for similar. Changed naming for your usecase.