递归循环数组

value:[
    {
        "parentId":0,
        "nodeId":20,
        "nodeLevel":1,
        "nodeName":"老板"
    },{
        "parentId":20,
        "nodeId":30,
        "nodeLevel":2,
        "nodeName":"黄经理"
    },{
        "parentId":20,
        "nodeId":32,
        "nodeLevel":2,
        "nodeName":"张经理"
    },{
        "parentId":30,
        "nodeId":37,
        "nodeLevel":3,
        "nodeName":"黄经理下属主管"
    },{
        "parentId":32,
        "nodeId":56,
        "nodeLevel":3,
        "nodeName":"张经理下属主管"
    }
]

上面是后端返回一个数组给我,要拼接成一个树型菜单
规则是看nodeLevel是几级和parentId是在那个nodeId下的
像上面老板parentId为0的规表示最高级
黄经理和张经理的parentId为20则表示他是老板下面的因为老板的nodeId为20
不管有多少级以次这样递归到最后一层展现成下面这样一个结构,这种要怎么去递归??

data: [
    {
        value: '老板'
        children: [
            {
                value:'黄经理',
                    children: [
                        {value: '黄经理下属主管'}
                    ]
            },
            {
                value:'张经理',
                    children: [
                        {value: '张经理下属主管'}
                    ]
            },
        ]
    }
]
阅读 3.5k
3 个回答

大概写了一下,应该可以改改,有些特殊情况没考虑吧

var value = [{
    "parentId": 0,
    "nodeId": 20,
    "nodeLevel": 1,
    "nodeName": "老板"
}, {
    "parentId": 20,
    "nodeId": 30,
    "nodeLevel": 2,
    "nodeName": "黄经理"
}, {
    "parentId": 20,
    "nodeId": 32,
    "nodeLevel": 2,
    "nodeName": "张经理"
}, {
    "parentId": 30,
    "nodeId": 37,
    "nodeLevel": 3,
    "nodeName": "黄经理下属主管"
}, {
    "parentId": 32,
    "nodeId": 56,
    "nodeLevel": 3,
    "nodeName": "张经理下属主管"
}];

var data = value.reduce(function(pre, cur) {
    pre = new Object(pre);
    cur = new Object(cur);
    return parse(pre, cur);
});

function parse(a, b) {
    if (a.parentId === b.nodeId) {
        if (b.children) {
            b.children.push(a);
        } else {
            b.children = [a];
        }
        return b;
    } else if (b.parentId === a.nodeId) {
        if (a.children) {
            a.children.push(b);
        } else {
            a.children = [b];
        }
        return a;
    } else {
        if (a.nodeLevel < b.nodeLevel && a.children) {
            for (let node of a.children) {
                let r = parse(node, b);
                if (r) {
                    a.children.map(v=>{
                        if (v.nodeId === r.nodeId) {
                            return r;
                        }
                        return v;
                    }
                    );
                }
            }
            return a;
        } else if (a.nodeLevel > b.nodeLevel && b.children) {
            for (let node of b.children) {
                let r = parse(node, a);
                if (r) {
                    b.children.map(v=>{
                        if (v.nodeId === r.nodeId) {
                            return r;
                        }
                        return v;
                    }
                    );
                }
            }
            return b;
        }
    }

}

function format(data) {
    if (!data) {
        return;
    }
    var r = {
        "value": data.nodeName
    };
    if (data.children) {
        r.children = [];
        for (let child of data.children) {
            r.children.push(format(child));
        }
    }
    return r;
}

data = format(data);

console.log(JSON.stringify(data));

有点意思,题主还在么,试试我这个?

    var data = [
        {
            "parentId":0,
            "nodeId":20,
            "nodeLevel":1,
            "nodeName":"老板"
        },{
            "parentId":20,
            "nodeId":30,
            "nodeLevel":2,
            "nodeName":"黄经理"
        },{
            "parentId":20,
            "nodeId":32,
            "nodeLevel":2,
            "nodeName":"张经理"
        },{
            "parentId":30,
            "nodeId":37,
            "nodeLevel":3,
            "nodeName":"黄经理下属主管"
        },{
            "parentId":32,
            "nodeId":56,
            "nodeLevel":3,
            "nodeName":"张经理下属主管"
        }
    ];

    function search(data,p_id){
        var temp = [];
        for(var index in data){
            if (data[index].parentId == p_id) {
                temp.push(data[index]);
            }
        }
        return temp;
    }

    function test(p_id = 0){
        var arr = [];
        var searchResult = search(data,p_id);
        for(var i in searchResult){
            arr.push({"value":searchResult[i].nodeName,"children":test(searchResult[i].nodeId)})
        }
        return arr;
    }

    console.log(test());

$(function(){

            var data =[
                {
                    "parentId":0,
                    "nodeId":20,
                    "nodeLevel":1,
                    "nodeName":"老板"
                },{
                    "parentId":20,
                    "nodeId":30,
                    "nodeLevel":2,
                    "nodeName":"黄经理"
                },{
                    "parentId":20,
                    "nodeId":32,
                    "nodeLevel":2,
                    "nodeName":"张经理"
                },{
                    "parentId":30,
                    "nodeId":37,
                    "nodeLevel":3,
                    "nodeName":"黄经理下属主管"
                },{
                    "parentId":32,
                    "nodeId":56,
                    "nodeLevel":3,
                    "nodeName":"张经理下属主管"
                }
            ];    
                var a={};
                 var b = {};
                 var c={};
                 var _data={};
                data.forEach(function(item,value){
                     
                     switch(item.nodeLevel){
                         case 1:
                                 a[item.nodeId] = item;
                         break;
                         case 2:
                                b[item.nodeId] = item;
                         break;
                         case 3:
                                 c[item.nodeId] = item;
                     }
                });
                for(var parentId in c){
                    if(!c.hasOwnProperty(parentId))continue;
                    var child = c[parentId];
                    if(typeof child.parentId != 'undefined' && child.parentId !=''){
                        if(typeof b[child.parentId].children == 'undefined' ){
                            b[child.parentId].children = [];
                        }
                            b[child.parentId].children.push(child);
                    }
                }
                for(var parentId in b){
                        if(!b.hasOwnProperty(parentId))continue;
                        var child = b[parentId];
                        if(typeof child.parentId != 'undefined' && child.parentId !=''){
                            if(typeof a[child.parentId].children == 'undefined' ){
                                a[child.parentId].children = [];
                            }
                                a[child.parentId].children.push(child);
                        }
                    }
            console.log(a);
        })
        试试这个 挺好玩的
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题