Sorry for the confusing title... I don't know a better summery.
I have an Array of Objects. Some of these Objects have a reference to it's parent Object. Something like that:
data:
[
{id: 2, parent: 1},
{id: 1},
{id: 3, parent: 1},
{id: 5, parent: 3},
{id: 4, parent: 3},
{id: 6, parent: 2}
]
What I want to do, is creating an Object out of this Array where the child objects are nested inside their parents. Like that:
data: {
id: 1,
children: [
{
id:2,
children: [
{id: 6}
]
},
{
id:3,
children: [
{id: 4},
{id: 5}
]
}
]
}
Does anyone know a smart way of doing this? I know I have to itterate through every Object of this Array and check if there is a parent. But how can I actually create this Object?