I have two objects with the next structure:
let sourceObj = [
{
items: [
{ items: [{ id: '0', name: 'z' }], name: 'm' },
{ items: [{ id: '2', name: 'q' }], name: 'l' },
],
name: 'c'
},
{
items: [
{ items: [{ id: '4', name: '-' }], name: 'v' },
],
name: 'd'
}
];
let targetObj = [
{
items: [
{ items: [{ id: '1', name: 'd' }], name: 'm' },
{ items: [{ id: '3', name: 'b' }], name: 'j' },
],
name: 'c'
}
];
I want to merge this object to get one object with the next structure:
let merged = [
{
items: [
{ items: [
{ id: '0', name: 'd' },
{ id: '1', name: 'z' }],
name: 'm'
},
{ items: [{ id: '2', name: 'q' }], name: 'l' },
{ items: [{ id: '3', name: 'b' }], name: 'j' },
],
name: 'c'
},
{
items: [
{ items: [{ id: '4', name: '-' }], name: 'v' },
],
name: 'd'
}
]
That is I want to get the object, which has joined arrays if the name of the source array is the same in the target array.
I tried use lodash method mergeWith, but I could join only upper items level of collections...
mergeWith(sourceObj, targetObj, (objValue, srcValue) => {
if (isArray(objValue)) {
return objValue.concat(srcValue);
}
});
targetObject = [{ id: "9", name: "c"}]? What would be the expected output?[{ items: [{ id: "9", name: "c"}], name: "-"}]