1. 程式人生 > >將扁平化的陣列還原成樹

將扁平化的陣列還原成樹

///將扁平化之後的陣列還原成樹
buildTree = (list) => {
let dataArray = [];
list.forEach(item=>{
if(item._parent_path ===''){
dataArray.push(item);
}
});
return this.data2treeDG(list, dataArray);
};
data2treeDG(datas, dataArray){
dataArray.forEach(item=>{
let childrenArray = [];
let CATL_CODEP = item._path;
datas.forEach(items=>{
let data = items;
let CATL_PARENT = data._parent_path;
if(CATL_CODEP===CATL_PARENT){
childrenArray.push(items);
}
});
item.children = childrenArray;
if(childrenArray.length > 0) {//有兒子節點則遞迴
this.data2treeDG(datas, childrenArray);
}
});
return dataArray;
}
};