有一个子父级结构的数据,需要过滤掉类型为1的数据
代码如下
//避免改变原数组
const arr = JSON.parse(JSON.stringify(this.tableData))
this.tableData = this.filterByGuid(arr)
//定义过滤方法
filterByGuid(tree) {
return tree.filter(node => {
if (node.type === '1' ) { // 过滤掉类型为1的数据
//返回false表示过滤数据
return false
}
// 递归处理子节点
if (node.children && node.children.length > 0 ) {
node.children = this.filterByGuid(node.children)
}
// 返回true表示保留当前节点
return true
}).filter(node => {
// 第二次过滤是为了确保在递归之后,没有子节点的 父节点也被删除
if (!node.children || node.children.length === 0 ) {
return false
}
return true
})
},