Can anyone help converting the following list of parent-child objects:
[
{
name:root,
_id:root_id,
},
{
name:a1,
parentAreaRef:{
id:root_id,
},
_id:a1_id,
},
{
name:a2,
parentAreaRef:{
id:a1_id,
},
_id:a2_id,
},
{
name:a3,
parentAreaRef:{
id:a2_id,
},
_id:a3_id,
},
{
name:b1,
parentAreaRef:{
id:root_id,
},
_id:b1_id,
},
{
name:b2,
parentAreaRef:{
id:b1_id,
},
_id:b2_id,
},
{
name:b3,
parentAreaRef:{
id:b1_id,
},
_id:b3_id,
}
]
into a tree structure showing the parent-child relationship:
[
{
name: root,
_id:root_id,
children: [
{
name: a1,
_id:a1_id,
children : [
{
name : a2,
_id:a2_id,
children : [
{
name : a3
_id:a3_id
}
]
}
]
},
{
name: b1,
_id:b1_id,
children : [
{
name : b2
_id:b2_id
},
{
name : b3
_id:b3_id
}
]
}
]
}
]
(The output structure is an array to allow for multiple roots but if we can get a solution that handles a single root that's great too.)
The output tree looks like this:
root
|
-- a1
| |
| -- a2
| |
| -- a3
|
-- b1
|
-- b2
-- b3
Thanks!