I have array of objects as tree,I need to add new property named "dateType" to the 3rd level
let tree = [
{
id: 1,
name: "parent1",
children: [
{
id: 2,
name: "child1",
children: [
{ id: 4, name: "subChild1" },
{ id: 5, name: "subChild2" }
]
},
{ id: 3, name: "child2" }
]
}
];
it should be like this:
let tree = [
{
id: 1,
name: "parent1",
children: [
{
id: 2,
name: "child1",
children: [
{ id: 4, name: "subChild1",dateType:"test" },
{ id: 5, name: "subChild2", dateType:"test" }
]
},
{ id: 3, name: "child2" }
]
}
];
how can I do that in easy way as my way it something like this:
custTree(res) {
let result = [];
res.forEach(level1 => {
level1.children.forEach(level2 => {
level2.children.forEach(level3 => {
console.log(level3)//nothing here
//code here
});
});
});
return result;
},
Note:the tree is dynamic and it is from 3 levels ,but alwayes I need to update the last level
treean array? What happens if it has two or more elements -- which one will be the root of the tree?