let tree = [{
name:"菜单1",
type:1,
id:1,
pid:0,
chlid:[
{
name:'菜单1-1',
type:1,
id:2,
pid:1,
chlid:[{
name:"按钮1",
type:2,
pid:2,
id:3
},{
name:"按钮1",
type:2,
pid:2,
id:4
}]
}
]
},{
name:"菜单1",
type:1,
id:5,
pid:0,
chlid:[
{
name:'菜单1-1',
type:1,
id:6,
pid:5,
chlid:[{
name:"按钮1",
type:2,
pid:6,
id:7
},{
name:"按钮1",
type:2,
pid:6,
id:8
}]
}
]
}]
const deleteTree = (data,type)=>{
data.forEach((e,i)=>{
if(e[type]==2){
delete data[i]
// console.log(111111,e)
}else if(e.chlid && e.chlid.length>0){
e.chlid = deleteTree(e.chlid,type)
}
})
return data.filter(e=>e)
}
console.log(tree)
tree = deleteTree(tree,'type')
console.log(tree)
console