I'm trying to transfer all the nodes of a child node to the parent using the spread operator or Object.assign (without relying on Lodash) while avoiding overwriting existing properties.
My initial thought was to simply append the childArray to the root object, but all my attempts have been unsuccessful.
const tree = {
rootonlyinfo: "test",
morefields: "another",
comments: [{
text: "This is comm 1",
id: "1"
},
{
text: "This is comm 2",
id: "2"
}
],
attachments: [{
text: "This is att 1",
id: "1"
},
{
text: "This is att 2",
id: "2"
}
],
children: [{
comments: [{
text: "This is comm 3",
id: "3"
}],
attachments: [{
text: "This att 3",
id: "3"
},
{
text: "This att 4",
id: "4"
}
],
children: [{
comments: [{
text: "This is comm 4",
id: "4"
}],
attachments: [{
text: "This is att 5",
id: "5"
}]
}]
}],
childArray: {
comments: [{
text: "This is comm from child 1",
id: "1"
},
{
text: "This is comm from child 2",
id: "2"
}
],
attachments: [{
text: "This is att from child 1",
id: "1"
},
{
text: "This is att from child 2",
id: "2"
}
],
children: [{
comments: [{
text: "This is comm from child 3",
id: "3"
}],
attachments: [{
text: "This att from child 3",
id: "3"
},
{
text: "This att from child 4",
id: "4"
}
],
children: [{
comments: [{
text: "This is comm from child 4",
id: "4"
}],
attachments: [{
text: "This is att from child 5",
id: "5"
}]
}]
}]
}
};
const mergedTree = [{ ...tree.childArray,
...tree
}];
console.log("mergedTree", mergedTree);
View code sample here: https://stackblitz.com/edit/angular-ivy-sxf2y4?file=src%2Fapp%2Fapp.component.ts
Any suggestions?
Expected Outcome:
{
"rootonlyinfo":"test",
"morefields":"another",
"comments":[
{
"text":"This is comm 1",
"id":"1"
},
{
"text":"This is comm 2",
"id":"2"
},
{
"text":"This is comm from child 1",
"id":"1"
},
{
"text":"This is comm from child 2",
"id":"2"
}
],
"attachments":[
{
"text":"This is att 1",
"id":"1"
},
{
"text":"This is att 2",
"id":"2"
},
{
"text":"This is att from child 1",
"id":"1"
},
{
"text":"This is att from child 2",
"id":"2"
}
],
"children":[
{
"comments":[
{
"text":"This is comm 3",
"id":"3"
},
{
"text":"This is comm from child 3",
"id":"3"
}
],
"attachments":[
{
"text":"This att 3",
"id":"3"
},
{
"text":"This att 4",
"id":"4"
},
{
"text":"This att from child 3",
"id":"3"
},
{
"text":"This att from child 4",
"id":"4"
}
],
"children":[
{
"comments":[
{
"text":"This is comm 4",
"id":"4"
}
],
"attachments":[
{
"text":"This is att 5",
"id":"5"
},
{
"text":"This is comm from child 4",
"id":"4"
},
{
"text":"This is att from child 5",
"id":"5"
}
]
}
]
}
],
"childArray":{
"comments":[
{
"text":"This is comm from child 1",
"id":"1"
},
{
"text":"This is comm from child 2",
"id":"2"
}
],
"attachments":[
{
"text":"This is att from child 1",
"id":"1"
},
{
"text":"This is att from child 2",
"id":"2"
}
],
"children":[
{
"comments":[
{
"text":"This is comm from child 3",
"id":"3"
}
],
"attachments":[
{
"text":"This att from child 3",
"id":"3"
},
{
"text":"This att from child 4",
"id":"4"
}
],
"children"[
{
"comments":[
{
"text":"This is comm from child 4",
"id":"4"
}
],
"attachments":[
{
"text":"This is att from child 5",
"id":"5"
}
]
}
]
}
]
}
}