1、问题描述:
将当前数组中的(`name: '/'`)删除掉,并将当前children中的数组合并到父数组去。
2、数据格式:
var data = [{
name: "A",
children: [{
name: "1",
children: [{
name: "01",
children: [{
name: "001",
children: []
}]
}]
}, {
name: "2",
pid: 1,
children: [{
name: "/",
pid: 2,
children: [{
name: "002",
pid: 3,
children: [{
name: "0002",
pid: 4,
children: [{
name: "/",
pid: 5,
children: [{
name: "000002",
pid: 6,
children: []
}
]
}]
}]
}]
}]
}, {
name: "/",
pid: 1,
children: [{
name: "/",
pid: 2,
children: [{
name: "/",
pid: 3,
children: [{
name: "0003",
pid: 4,
children: []
},{
name: "0003",
pid: 4,
children: []
}]
}]
}]
}]
}]
3、处理:通过递归方式,深度查找
function filter(old) {
let arr = [];
old && old.forEach((item, index)=> {
if(item.name === '/') {
arr.push(...filter(item.children))
} else {
item.children = filter(item.children)
arr.push(item);
}
})
return arr;
}
console.log(filter(data))
4、结果: