将一层数据处理成树形结构
作者:互联网
convert(list){
const map = list.reduce((res,v)=>{
res[v.id] = v;
return res;
},{})
const res = [];
for(let item of list){
if(item.parentId === 0){
res.push(item);
continue;
}
if(item.parentId in map){
const parent = map[item.parentId];
parent.children = parent.children || [];
parent.children.push(item);
}
}
return res;
},
标签:一层,map,const,parent,res,item,树形,parentId,数据处理 来源: https://blog.csdn.net/weixin_48058348/article/details/122104076