js代码:把线性数据转成树形数据
function setTreeData(data){
let cloneData = JSON.parse(JSON.stringify(data))
let tree = cloneData.filter((father)=>{
let branchArr = cloneData.filter((child)=>{
return father.id == child.parentId
});
if(branchArr.length>0){
father.children = branchArr;
}
return father.parentId==0;
});
return tree
}
网友评论