I need help looping through the following object array to reorganize everything into a hierarchy/tree. How do I achieve this? Any help is appreciated.
[
{
"id":1,
"parentId":null,
"value":"Root Parent 01",
"children":[]
},
{
"id":8,
"parentId":null,
"value":"Root Parent 02",
"children":[]
},
{
"id":36,
"parentId":15,
"value":"Child 01",
"children":[]
},
{
"id":43,
"parentId":15,
"value":"Child 02",
"children":[]
},
{
"id":50,
"parentId":15,
"value":"Child 03",
"children":[]
},
{
"id":15,
"parentId":null,
"value":"Root Parent 03",
"children":[]
},
{
"id":57,
"parentId":22,
"value":"Child 04",
"children":[]
},
{
"id":64,
"parentId":22,
"value":"Child 05",
"children":[]
},
{
"id":71,
"parentId":22,
"value":"Child 06",
"children":[]
},
{
"id":78,
"parentId":22,
"value":"Child 07",
"children":[]
},
{
"id":85,
"parentId":22,
"value":"Child 08",
"children":[]
},
{
"id":92,
"parentId":22,
"value":"Child 09",
"children":[]
},
{
"id":99,
"parentId":22,
"value":"Child 10",
"children":[]
},
{
"id":106,
"parentId":22,
"value":"Child 11",
"children":[]
},
{
"id":22,
"parentId":null,
"value":"Root Parent 04",
"children":[]
},
{
"id":113,
"parentId":29,
"value":"Child 12",
"children":[]
},
{
"id":115,
"parentId":29,
"value":"Child 13",
"children":[]
},
{
"id":122,
"parentId":29,
"value":"Child 14",
"children":[]
},
{
"id":129,
"parentId":29,
"value":"Child 15",
"children":[]
},
{
"id":136,
"parentId":29,
"value":"Child 16",
"children":[]
},
{
"id":143,
"parentId":29,
"value":"Child 17",
"children":[]
},
{
"id":157,
"parentId":150,
"value":"Child 18",
"children":[]
},
{
"id":150,
"parentId":29,
"value":"Child 19",
"children":[]
},
{
"id":29,
"parentId":null,
"value":"Root Parent 05",
"children":[]
}
]
Desired object array:
[
{
"id":1,
"parentId":null,
"value":"Root Parent 01",
"children":[]
},
{
"id":8,
"parentId":null,
"value":"Root Parent 02",
"children":[]
},
{
"id":15,
"parentId":null,
"value":"Root Parent 03",
"children":[
{
"id":36,
"parentId":15,
"value":"Child 01",
"children":[]
},
{
"id":43,
"parentId":15,
"value":"Child 02",
"children":[]
},
{
"id":50,
"parentId":15,
"value":"Child 03",
"children":[]
}
]
},
{
"id":22,
"parentId":null,
"value":"Root Parent 04",
"children":[
{
"id":57,
"parentId":22,
"value":"Child 04",
"children":[]
},
{
"id":64,
"parentId":22,
"value":"Child 05",
"children":[]
},
{
"id":71,
"parentId":22,
"value":"Child 06",
"children":[]
},
{
"id":78,
"parentId":22,
"value":"Child 07",
"children":[]
},
{
"id":85,
"parentId":22,
"value":"Child 08",
"children":[]
},
{
"id":92,
"parentId":22,
"value":"Child 09",
"children":[]
},
{
"id":99,
"parentId":22,
"value":"Child 10",
"children":[]
},
{
"id":106,
"parentId":22,
"value":"Child 11",
"children":[]
}
]
},
{
"id":29,
"parentId":null,
"value":"Root Parent 05",
"children":[
{
"id":113,
"parentId":29,
"value":"Child 12",
"children":[]
},
{
"id":115,
"parentId":29,
"value":"Child 13",
"children":[]
},
{
"id":122,
"parentId":29,
"value":"Child 14",
"children":[]
},
{
"id":129,
"parentId":29,
"value":"Child 15",
"children":[]
},
{
"id":136,
"parentId":29,
"value":"Child 16",
"children":[]
},
{
"id":143,
"parentId":29,
"value":"Child 17",
"children":[]
},
{
"id":150,
"parentId":29,
"value":"Child 19",
"children":[
{
"id":157,
"parentId":150,
"value":"Child 18",
"children":[]
}
]
}
]
}
]
Desired Tree:
Root Parent 01
Root Parent 02
Root Parent 03
---Child 01
---Child 02
---Child 03
Root Parent 04
---Child 04
---Child 05
---Child 06
---Child 07
---Child 08
---Child 09
---Child 10
---Child 11
Root Parent 05
---Child 12
---Child 13
---Child 14
---Child 15
---Child 16
---Child 17
---Child 19
------Child 18