I have this data structure that shows the depth of each node in a nested tree:
[
{
"name": "ELECTRONICS",
"depth": 0
},
{
"name": "TELEVISIONS",
"depth": 1
},
{
"name": "TUBE",
"depth": 2
},
{
"name": "PLASMA",
"depth": 2
},
{
"name": "GAME CONSOLES",
"depth": 1
},
{
"name": "MP3 PLAYERS",
"depth": 1
},
{
"name": "FLASH",
"depth": 2
}]
I would like to convert the preview data using JavaScript / node.js / Angular to hierarchical JSON as follows:
[{
"name": "ELECTRONICS",
"children": [
{
"name": "TELEVISIONS",
"children": [
{
"name": "TUBE"
},
{
"name": "PLASMA"
}]
},
{
"name": "GAME CONSOLES"
},
{
"name": "MP3 PLAYERS",
"children": [
{
"name": "FLASH"
}]
}]
}]
source
share