I am finding a difficult time finding a solution for multiple levels of push / merging with Lodash and Underscore. Trying to avoid some messy nested loops with JS.
Here's an example of how I need to merge.
const arr = [
{
level : 'test',
items : [1, 2, 3]
},
{
level : 'tests',
items : [1, 2, 3]
}
];
const obj = {
level : 'test',
items : [4, 5, 6]
};
/* Output:
[
{
level : 'test',
items : [1, 2, 3, 4, 5, 6]
},
{
level : 'tests',
items : [1, 2, 3]
}
];
*/
The obj
level matches arr[0]
, and so the items array should merge. New, or unique levels should push to the array as a new object.
Is there a way I could achieve this through some combination of _.groupBy and _.mergeWith of Lodash? So far, I have gotten it to merge into a single array with two objects from the two respective unique levels, but when the items arrays merge it ends up with [4, 5, 6]
.
Any help would be appreciated.