I am struggling to navigate the intricate nested tree view of child items within a JSON array. I have been grappling with this challenge for days, trying to figure out how to access multiple children from the complex JSON structure. Can someone provide guidance on how to access all children using Angular or JavaScript? Additionally, how can I identify which children belong to which parent? So far, I've only managed to retrieve the children of the first parents using the code snippet below:
assigning the JSON to value=datasource.data;
this.dataSource.data.forEach((item,i)=>{
console.log(item.children);
if(item.children){
item.children.forEach((childItems,i)=>{
console.log(childItems);
})
}
Here is the JSON:
TREE_DATA: FoodNode[] =
[
{
name: 'Dashboard',
id: "0",
startButton: "enabled",
stopButton: "enabled",
type: "ec2",
children: [
{
name: 'Backend-Server',
id: "1",
startButton: "enabled",
stopButton: "enabled",
type: "ec2",
children: [
{
startButton: "disabled",
stopButton: "enabled",
type: "ec2",
name: 'Backend-Server-1',
id: "3"
},
{
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Backend-Server-2',
id: "4"
},
]
},
{
startButton: "enabled",
stopButton: "disabled",
type: "rds",
name: 'Frontend-Server',
id: "5"
},
{
startButton: "enabled",
stopButton: "enabled",
type: "ec2",
name: 'Backup-Server',
id: "6"
},
]
},
{
name: 'Admin',
id: "7",
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
children: [
{
name: 'Backend-Server',
id: "8",
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
children: [
{
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Backend-Server-1',
id: "9"
},
{
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Backend-Server-2',
id: "10"
},
]
}, {
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Frontend-Server',
id: "11",
children: [
{
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Frontend-Server-1',
id: "12",
},
{
startButton: "enabled",
stopButton: "disabled",
type: "ec2",
name: 'Frontend-Server-3',
id: "13"
},
]
},
]
},
]