I have a typical organization hierarchy. For example.
D,E is reporting to B. B,C is reporting to A.
A is the topmost node. But I get this data as a flat array with an attribute pointing to the parent.
[{
name: "A",
parent: null
},
{
name: "B",
parent: "A"
},
{
name: "C",
parent: "A"
},
{
name: "D",
parent: "B"
},
{
name: "E",
parent: "B"
}]
But I want to convert this to single nested objector tree. The root node has a child attribute with built-in children, and each child has its own child attribute similar to this.
{
name: "A",
children: [{
name: "C"
children: [{
name: "D"
},{
name: "E"
}]
},{
name: "C"
}]
}
How can I do this in javascript efficiently?
Rahul source
share