const array = [
{
title: "标题1",
key: 1,
role: ['one', 'two', 'three'],
},
{
title: "标题2",
key: 2,
role: ['one', 'two', 'three'],
children: [
{
title: "标题2-1",
key: "2-1",
role: ['one', 'two', 'three'],
},
{
title: "标题2-2",
key: "2-2",
role: ['one', 'two'],
},
{
title: "标题2-3",
key: "2-3",
role: ['one', 'three'],
}
]
}
]
function Filter(array, itea) {
let newArray = []
for (let item of array) {
if (item.role.includes(itea)) {
let obj=item
if(item.children){
obj.children=Filter(item.children,itea)
}
newArray.push(obj)
}
}
return newArray
}
console.log(Filter(array, 'two'));
数组递归过滤
最新推荐文章于 2024-05-10 14:50:14 发布