I have typical organization hierarchy. For example.
D,E is reporting to B. B,C is reporting to A.
A is the top most node. But I receive 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 in to single nested object or a tree. A root node has children attribute with the children embedded and each child has its own children attribute like this.
{
name: "A",
children: [{
name: "C"
children: [{
name: "D"
},{
name: "E"
}]
},{
name: "C"
}]
}
How can I do this in javascript efficiently?