一维数组转化成多维数组

由于要转化成的数组层级不固定,所以我们直接使用for循环嵌套就不理想了,下面是我写出来的方法

 let egs = [
            { name_1: 'name_1', id:1 },
            { name_2: 'name_2', id: 2, pid: 1 },
            { name_3: 'name_3', id: 3, pid: 1 },
            { name_4: 'name_4', id: 4, pid: 2 },
            { name_5: 'name_5', id: 5, pid: 3 },
            { name_6: 'name_6', id: 6, pid: 4 },
            { name_7: 'name_7', id: 7, pid: 6 },
            { name_8: 'name_8', id: 8, pid: 7 },
            { name_9: 'name_9', id: 9, pid: 8 },
            { name_10: 'name_10', id: 10, pid: 9 },
            { name_11: 'name_11', id: 11, pid: 10 },
        ]
        let result = { name_0: 'name_0', id: 0 }
        let key = 'children'
        let relation='pid'
        let ParentId='id'
        s(egs)
        function s(arr) {
            arr.filter((o, i) => {
                if(!o[relation]){
                    if (result[key] && result[key][0]) {
                    } else {
                        result[key] = []
                    }
                    result[key].push(o)
                }else{
                    if(result.children&&result.children[0]){
                        s1(result.children,o)
                    }
                }
            })
        }   
        function s1(result,o) {
            for(let k in result){
                if(result[k][ParentId]==o[relation]){
                    if(result[k].children&&result[k].children[0]){
                        result[k].children.push(o)
                    }else{
                        result[k].children=[]
                        result[k].children.push(o)
                    }
                    break
                }else{
                    if(result[k].children&&result[k].children[0]){
                        s1(result[k].children,o)
                    }
                }
            }
        }
        console.log(result)

打印出的结果

        {
            "name_0":"name_0", "id":0,
            "children":[
            {
                "name_1":"name_1",  "id":1,
                "children":[
                    {
                        "name_2":"name_2", "id":2,  "pid":1,
                        "children":[
                            {
                                "name_4":"name_4", "id":4, "pid":2,
                                "children":[
                                    {
                                        "name_6":"name_6", "id":6, "pid":4,
                                        "children":[
                                            {
                                                "name_7":"name_7", "id":7, "pid":6,
                                                "children":[
                                                    {
                                                        "name_8":"name_8", "id":8, "pid":7,
                                                        "children":[
                                                            {
                                                                "name_9":"name_9", "id":9, "pid":8,
                                                                "children":[
                                                                    {
                                                                        "name_10":"name_10", "id":10, "pid":9,
                                                                        "children":[
                                                                            {
                                                                                "name_11":"name_11", "id":11, "pid":10
                                                                            }
                                                                        ]
                                                                    }
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "name_3":"name_3", "id":3, "pid":1,
                        "children":[
                            {
                                "name_5":"name_5", "id":5, "pid":3
                            }
                        ]
                    }
                ]
            }
        ]
        }

你可能感兴趣的:(一维数组转化成多维数组)