I have a flat array like this, I'm supposed to build a flat array for it. The object will be an children property of it's parent object if the pid is not null. How should I do this?
var a = [
{id: 1, pid: null},
{id: 2, pid: 1},
{id: 3, pid: 1},
{id: 4, pid: 3},
{id: 5, pid: 3}
];
Expect output:
var result = [{id: 1, children: [
{id: 2, children: []},
{id: 3, children: [{id: 4}, {id: 5}]}
]}]