数组对象删除不满足某些条件的对象 js
recursiveFunction(items, childrenNodeName, ids) {
console.log('items', ids);
// 获取数组长度
if (items) items = [];
let len = items?.length
// 循环遍历数组
for (let i = 0; i < len; i++) {
// 如果有子节点,递归遍历子节点
if (items[i][childrenNodeName].length > 0) {
this.recursiveFunction(items[i][childrenNodeName], childrenNodeName, ids)
}
// 删除不满足某些条件的节点,删除后,当前数组下标、长度均减一(这里条件是:
// 当前项 id)
console.log('ids.includes(items[i].id)', ids.includes(items[i].id));
if (!ids.includes(items[i].id)) {
items.splice(i, 1)
len--
i--
}
}
},
filter(data, id) {
var newData = data.filter(x => x.id === id)
newData.forEach(x => x.children && (x.children = this.filter(x.children, id)))
return newData
},