js将树形结构的数组扁平化(按顺序)


    treeData(arr) {
        let cloneData = JSON.parse(JSON.stringify(arr))   //先将原来的数组深拷贝一份,防止影响到原来的数据
        return cloneData.filter(father => {
            let branchArr = cloneData.filter(child => father.id == child.parentId);
            branchArr.length > 0 ? father.children = branchArr : ''
            return father.parentId == null    //返回第一层的数据,如果最上一级的父级id不是null,请自行修改
        })
    }
 
调用 this.treeData(json数组)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM