I'm trying to take this array of objects:
var channels = [{
cid: 5,
pid: 10
},
{
cid: 10,
pid: 0
},
{
cid: 20,
pid: 5
},
{
cid: 15,
pid: 10
}];
cid = channel Id, pid = parent channel Id.
I need to group the channels under their parents, however, those channels could also be parents. How can I group them recursively to look like below?:
var data = [{
cid: 10,
pid: 0,
sub_channels: [{
cid: 5,
pid: 10,
sub_channels: [{
cid: 20,
pid: 5
}]
},
{
cid: 15,
pid: 10
}];
}];
I'm using NodeJS so I'm very open to trying any modules, or javascript libraries to get this to work.