I have this object:
[
{
id: 1,
child: false,
link_type: expand
},
{
id: 2,
child: true,
url: /home,
link_type: direct,
parent: 1
},
{
id: 3,
child: false,
url: /settings,
link_type: direct
}
]
I want to do nesting of the object in such a way that the final output will be:
[
{
id: 1,
link_type: expand,
child: [
{
id: 2,
url: /home,
link_type: direct,
parent: 1
}
]
},
{
id: 3,
url: /settings,
link_type: direct
}
]
note: the orignal data has other fields I have included fields that are needed.
I have tried creating a nesting like this for parent node:
this.sideMenuService.getAll().subscribe(d => {
console.log(JSON.stringify(d));
let data: SideMenuList = {};
let count = 0;
d.forEach(e => {
if (!e.child) {
// console.log(count);
// console.log(e.menu_id);
data.menu_id = e.menu_id;
data.displayname = e.displayname;
data.iconcss = e.iconcss;
data.link_type = e.link_type;
data.sequence = e.sequence;
data.url = e.url;
console.log(data);
this.sideMenuList.push(data);
// console.log(this.sideMenuList);
count++;
}
});
console.log(this.sideMenuList);
});
But i am getting output like this:

Any help will be appreciated.