I'm trying to recursively traverse the json tree and extract all leaf nodes (nodes with children=null) and return a flat list. I'm not getting the full lis. I'm only getting one item. I think I'm almost there but I can't quite figure what mistake I'm making here. Please advise.
let cluster = {
"children": [
{
"children": [
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "engrlab-129-138.engrlab.com",
"resourceId": "13635212858517569813",
"resourceType": "host",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
}
],
"expanded": true,
"selected": false,
"resourceName": "Hosts",
"resourceId": "hosts_1",
"resourceType": "hosts",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 3
},
{
"children": [
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Modules",
"resourceId": "15637959608568429034",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Schemas",
"resourceId": "13047253143282887930",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Meters",
"resourceId": "17708194274661652566",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Last-Login",
"resourceId": "13804286217083551733",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Triggers",
"resourceId": "16540036655917736056",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Security",
"resourceId": "15719129614781070270",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Fab",
"resourceId": "15346546234841845662",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Documents",
"resourceId": "8955207084521064900",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Extensions",
"resourceId": "16902376358803738229",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "opsdir-modules",
"resourceId": "17671323036239623175",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "App-Services",
"resourceId": "13845862599166782540",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "OpsDirector",
"resourceId": "11662986908968996065",
"resourceType": "database",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 4
}
],
"expanded": true,
"selected": false,
"resourceName": "Databases",
"resourceId": "databases_2",
"resourceType": "databases",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 3
},
{
"children": [
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Manage",
"resourceId": "8298375594821661846",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "OpsDirectorIO",
"resourceId": "17083255384250121954",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "App-Services",
"resourceId": "15123715826272873087",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "Admin",
"resourceId": "15145610355571980588",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "HealthCheck",
"resourceId": "15612145677080623630",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
},
{
"children": null,
"expanded": true,
"selected": false,
"resourceName": "OpsDirector",
"resourceId": "18324479201748050347",
"resourceType": "server",
"clusterId": "11227628265937443823",
"group": "Default",
"health": null,
"level": 4
}
],
"expanded": true,
"selected": false,
"resourceName": "App Servers",
"resourceId": "servers_3",
"resourceType": "servers",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 3
}
],
"expanded": true,
"selected": false,
"resourceName": "engrlab-129-138.engrlab.com-cluster",
"resourceId": "4478300518534191471",
"resourceType": "cluster",
"clusterId": "4478300518534191471",
"group": null,
"health": null,
"level": 2
};
function getNodeList(nodes, result = []) {
nodes = [].concat(nodes);
result = [].concat(result);
for (var i = 0; i < nodes.length; i++) {
if(nodes[i].children) {
return getNodeList(nodes[i].children, result)
}
if(!nodes[i].children) result.push(nodes[i]);
};
return result;
}
getNodeList(cluster)
Did a bit of modification to your function. I think this returns all leaf nodes.
let cluster {...};
function getLeafNodes(nodes, result = []){
for(var i = 0, length = nodes.length; i < length; i++){
if(!nodes[i].children || nodes[i].children.length === 0){
result.push(nodes[i]);
}else{
result = getLeafNodes(nodes[i].children, result);
}
}
return result;
}
getLeafNodes([cluster]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With