gpt4 book ai didi

javascript - 从深度未知的嵌套数组中删除项目

转载 作者:行者123 更新时间:2023-12-05 00:31:03 25 4
gpt4 key购买 nike

我正在尝试根据正确匹配的数组从嵌套数组中删除项目。
适用三个要求:

  • 阵列的深度未知。项可以有嵌套的子项。
  • 只应删除没有子项的项目
  • 如果项目不在匹配数组中,则应删除它们

  • 我已经构建了一个函数来递归地到达最深层次并根据 $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 just return the array as-is
    return arr
    }
    }

    let arr = [
    {
    'name': 'John',
    'children': [
    {
    'name': 'John',
    'children': [
    { 'name': 'John' },
    { 'name': 'Jane' },
    { 'name': 'Joe' }
    ]
    }]
    }, {
    'name': 'Jeff',
    'children': [
    {
    'name': 'Joe',
    'children': [
    { 'name': 'Jill' },
    { 'name': 'Jeff' },
    { 'name': 'Joe' }
    ]
    }]
    }];

    let match = ['John', 'Joe'];
    let result = filterRecursively(arr, match);

    console.log(result);

    // Expected result:
    [
    {
    'name': 'John',
    'children': [
    {
    'name': 'John',
    'children': [
    { 'name': 'John' },
    { 'name': 'Joe' }
    ]
    }]
    }, {
    'name': 'Jeff',
    'children': [
    {
    'name': 'Joe',
    'children': [
    { 'name': 'Joe' }
    ]
    }]
    }];
    // Current output
    [
    {
    "name": "Joe"
    }
    ]
    See the Codepen

    最佳答案

    因为forEach基本上“跳过”层而不返回任何东西,你最终只会得到你的第一个也是最深的结果。
    我还认为您的函数有点复杂,因为它以数组开头,而不是一种 ROOT节点。
    这是(我认为)满足您要求的替代方案:

    let childlessMatch = (node, match) => {
    // If it's at the deepest level, check against match
    if (node.children === undefined) {
    return match.includes(node.name) ? [node] : [];
    }

    // If not, calculate the next child layer first
    const newChildren = node.children.flatMap(c => childlessMatch(c, match));

    // With the children calculated, we can prune based on whether there
    // are any children left to show
    if (newChildren.length === 0) return [];

    return [{
    ...node,
    children: newChildren
    }]
    }
    在一个可运行的片段中:

    let childlessMatch = (node, match) => {
    if (node.children === undefined) {
    return match.includes(node.name) ? [node] : [];
    }

    const newChildren = node.children.flatMap(c => childlessMatch(c, match));
    if (newChildren.length === 0) return [];

    return {
    ...node,
    children: newChildren
    }
    }

    let arr = [
    {
    'name': 'John',
    'children': [
    {
    'name': 'John',
    'children': [
    { 'name': 'John' },
    { 'name': 'Jane' },
    { 'name': 'Joe' }
    ]
    }]
    }, {
    'name': 'Jeff',
    'children': [
    {
    'name': 'Joe',
    'children': [
    { 'name': 'Jill' },
    { 'name': 'Jeff' },
    { 'name': 'Joe' }
    ]
    }]
    }];

    let match = ['John', 'Joe'];
    let result = childlessMatch({ children: arr }, match).children;

    console.log(result);

    关于javascript - 从深度未知的嵌套数组中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72489407/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com