美文网首页
将树形结构转换为数组

将树形结构转换为数组

作者: Enjoyhy | 来源:发表于2021-02-26 14:29 被阅读0次
    treeConvertToArr(tree) {
                let arrs = [];
                let result = [];
                arrs = arrs.concat(tree);
                while (arrs.length) {
                    let first = arrs.shift(); // 弹出第一个元素
                    if (first.children) {
                    //如果有children
                    arrs = arrs.concat(first.children);
                    delete first["children"];
                    }
                    result.push(first);
                }
                return result;
            },
    
    
    WeChate2d96059c0f6ecd59564079649d15b80.png
    this. treeConvertToArr(result.data)
    console.log(this. treeConvertToArr(result.data))//得到[{id:"1",deptName:"长安福特"},{d:"2",deptName:"福特一厂"},{d:"3",deptName:"福特二厂"},{d:"4",deptName:"福特三厂"}]
    
    

    相关文章

      网友评论

          本文标题:将树形结构转换为数组

          本文链接:https://www.haomeiwen.com/subject/icimfltx.html