How can I sort this array?
I am trying to group an array of objects from a parentId
that each element of the array brings, there can always be an indeterminate number of levels, the result I expect is the one I have in the output.
Input:
[
{
"_id": "123",
"name": "ABC",
"parentID": ""
},
{
"_id": "645",
"name": "ABC 2",
"parentID": "123"
},
{
"_id": "65",
"name": "ABC 3",
"parentID": ""
}
]
Output:
[
{
"_id": "123",
"name": "ABC",
"parentID": "",
"children": [
{
"_id": "645",
"name": "ABC 2",
"parentID": "123"
},
]
},
{
"_id": "65",
"name": "ABC 3",
"parentID": ""
}
]
Thanks for help