I have below an array
{
"sec": "11",
"details": [
{
"id": "1",
"user": "Me1"
},
{
"id": "2",
"uesr": "Me2"
},
{
"id": "3",
"user": "Me3"
}
{
"id": "4",
"user": "Me4",
parentID:"2"
},
{
"id": "5",
"uesr": "Me5"
},
{
"id": "6",
"user": "Me6",
parentID:"2"
}
{
"id": "7",
"user": "Me7"
},
{
"id": "8",
"uesr": "Me8",
parentID:"7"
},
{
"id": "9",
"user": "Me9",
parentID:"7"
}
],
"isDisplay": "true"
}
and output should be like below
{
"sec":"11",
"details":[
{
"id":"1",
"user":"Me1"
},
{
"id":"2",
"uesr":"Me2",
"childs":[
{
"id":"4",
"user":"Me4",
"parentID":"2"
},
{
"id":"6",
"user":"Me6",
"parentID":"2"
}
]
},
{
"id":"3",
"user":"Me3"
},
{
"id":"5",
"uesr":"Me5"
},
{
"id":"7",
"user":"Me7",
"childs":[
{
"id":"8",
"uesr":"Me8",
"parentID":"7"
},
{
"id":"9",
"user":"Me9",
"parentID":"7"
}
]
}
],
"isDisplay":"true"
}
I can do this by simple looping,
In lodash or anything angular does this functionality.
I am clueless to start, I just give below code
this.list = _.groupBy(this.list,"parentID");
But the output not as expected.
Please help or guide
Thanks
You need a different approach, not grouping, but creating a tree out of the related data.
This solution uses an array with
id
as key and withparentID
as well. The code works with a single loop, because of storing of the relation of children and parent and parent to their children.