js树结构组装

2021-03-15  本文已影响0人  背后_542d
 setTreeData(source) {
            let cloneData = JSON.parse(JSON.stringify(source))
            return cloneData.filter(father => {
                // 循环所有项,并添加children属性
                father.id = father.ID;
                father.label = father.ClassificationName;
                let branchArr = cloneData.filter(child => {
                    child.id = child.ID;
                    child.label = child.ClassificationName;
                    return father.ID == child.ParentID
                });   // 返回每一项的子级数组
                branchArr.length > 0 ? father.children = branchArr : ''   //给父级添加一个children属性
                return father.ParentID == null;      //返回第一层,
            });
        },

上一篇下一篇

猜你喜欢

热点阅读