var categoryName = 's'
function filterTree(node) {
if (!(node.subcategoryList && node.subcategoryList.length)) {
return node.categoryName.includes(categoryName)
}
node.subcategoryList = node.subcategoryList.filter(filterTree)
if (node.categoryName.includes(categoryName)) {
return true
}
return node.subcategoryList.length > 0
}
var tree = [
{
categoryName: 'zhangsna',
subcategoryList: [
{
categoryName: 'zhangsi',
subcategoryList: []
}
]
},
{
categoryName: 'lisi',
subcategoryList: [{ categoryName: 'liwu' }]
}
]
var res = tree.filter(filterTree)
console.log(res)
console