树形结构数据,对三级数据进行filter过滤时,遇到的问题:
forEach、filter方法均不会改变原数组
this.cate为树形数据
错误代码:这种拿不到this.cate,this.cate会显示undefined
// 过滤掉禁投的三级
this.cate = this.cate.forEach(catelv1 => {
catelv1['children'].forEach(catelv2 => {
catelv2['children'].filter(catelv3 => {
return !catelv3.banCategory
})
})
})
正确代码:需要使用catelv2[‘children’]接一下
// 过滤掉禁投的三级
this.cate.forEach(catelv1 => {
catelv1['children'].forEach(catelv2 => {
catelv2['children'] = catelv2['children'].filter(catelv3 => {
return !catelv3.banCategory
})
})
})
// 过滤掉没有三级的二级
this.cate.forEach(item => {
item['children'] = item['children'].filter(val => {
return val.children.length
})
})