我正在尝试根据正确匹配的数组从嵌套数组中删除项目。
适用三个要求:
我构建了一个函数来递归地到达最深层次并根据 $match 数组过滤项目。
这是我的代码到目前为止的样子:
import * as lodash from "https://cdn.skypack.dev/lodash@4.17.21";
let filterRecursively = (arr, match) => {
// Recursively go to the deepest array we can find
arr.forEach(el => {
arr = el.children ? filterRecursively(el.children, match) : arr
});
// If we are at the deepest level we filter the items ...
if (arr[0] && arr[0].children === undefined) {
return _.filter(arr, (item) => {
return match.includes(item.name)
})
} else { // ... if not we …Run Code Online (Sandbox Code Playgroud)