用于提取所有叶节点的递归函数

时间:2017-02-14 01:07:40

标签: javascript recursion

我尝试以递归方式遍历json树并提取所有叶节点(带有children = null的节点)并返回一个平面列表。我没有得到完整的lis。我只得到一件物品。我想我几乎就在那里,但我无法确定我在这里犯的错误。请指教。

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)

4 个答案:

答案 0 :(得分:3)

对您的功能进行了一些修改。我认为这将返回所有叶节点。

let cluster {...};

function getLeafNodes(nodes, result = []){
  for(var i = 0, length = nodes.length; i < length; i++){
    if(!nodes[i].children){
      result.push(nodes[i]);
    }else{
      result = getLeafNodes(nodes[i].children, result);
    }
  }
  return result;
}

getLeafNodes([cluster]);

答案 1 :(得分:3)

另一个代码:

function getLeafNodes(nodes){
    function traverse(acc, node){
        if(node.children) 
            return node.children.reduce(traverse, acc);
        acc.push(node);
        return acc;
    }

    return traverse([], nodes);
}

getLeafNodes(cluster);

答案 2 :(得分:1)

这将返回null个孩子的所有节点:

function getNodeList(object, list) {
    if (object.children) {
        for (var i = 0; i < object.children.length; i++) {
            getNodeList(object.children[i], list);
        }
    }
    else {
        list.push(object);
    }
}

list = []
getNodeList(cluster, list)
for (var i = 0; i < list.length; i++) {
    console.log(list[i])
}

请注意,我将list作为参数传递而不是返回。我认为这是你出错的地方。

答案 3 :(得分:1)

递归是一种功能性遗产,因此将其与功能性样式一起使用可产生最佳效果-

const 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}
  
const leafs = (t = {}) =>
  t.children
    ? t.children.flatMap(leafs)
    : [ t ]

const result =
  leafs(cluster)
  
console.log(JSON.stringify(result))