I have the following array:
const arr = [
{
id:"aaa",
name:"aaa",
type:"director",
nodes:[
{
type:"manager",
id:"111",
name:"111"
}
]
},
{
id:"aaa",
name:"bbb",
type:"director",
nodes:[
{
type:"manager",
id:"222",
name:"222"
}
]
},
{
id:"aaa",
name:"aaa",
type:"director",
nodes:[
{
type:"manager",
id:"333",
name:"333"
}
]
},
{
id:"aaa",
name:"bbb",
type:"director",
nodes:[
{
type:"manager",
id:"444",
name:"444"
}
]
}
]
desired output:
const arr = [
{
id:"aaa",
name:"aaa",
type:"director",
nodes:[
{
type:"manager",
id:"111",
name:"111"
},
{
type:"manager",
id:"333",
name:"333"
}
]
},
{
id:"aaa",
name:"bbb",
type:"director",
nodes:[
{
type:"manager",
id:"222",
name:"222"
},
{
type:"manager",
id:"444",
name:"444"
}
]
}
I am trying to merge the value of the array of objects of nodes that have the same key-value(id, name, and type) pairs in the above array to the desired output.
I have tried the following but in vain with the arr.reduce, but in vain:
arr.reduce((acc, el) => {
var existEl = acc.find(e => e.nodes.id === el.nodes.id);
if (existEl) {
existEl = el;
} else {
acc.push(el);
}
return acc;
}, []);
Any help would be much appreciated. Thanks in advance.
id,name, andtype, or "everything-but-nodes", or something else altogether?