let arr = [
{
id:1,
pid:0,
title:"第一层一级"
},
{
id:2,
pid:1,
title:"第一层二级"
},
{
id:3,
pid:0,
title:"第二层一级"
},
{
id:4,
pid:3,
title:"第二层二级"
},
{
id:5,
pid:4,
title:"第二层三级"
},
{
id:6,
pid:0,
title:"第二层三级"
}
]
arr.forEach((item)=>{
if(item.pid!==0){
const findParent = arr.find(i=>i.id === item.pid);
if(findParent){
if(!Array.isArray(findParent.children)){
findParent.children = []
}
findParent.children.push(item)
}
}
})
const treeArr = arr.filter((i)=>i.pid === 0);
console.log(treeArr)
js处理目录树简单方法
最新推荐文章于 2023-11-29 15:25:08 发布