js 数据数组处理

已知:两个数组

 let list= [1, 21, 22, 3,31,412,43]
 let admin= [
    {
      id: 1,
      title: "首页",
    },
    {
      id: 2,
      title: "系统管理",
      children: [
        {
          id: 21,
          title: "个人信息",
        },
        {
          id: 22,
          title: "用户管理",
        },
        {
          id: 23,
          title: "权限配置",
        }
      ]
    },
    {
      id: 3,
      title: "基础配置",
      children: [
        {
          id: 31,
          title: "数据字典",
        }
      ]
    },
    {
      id: 4,
      title: "业务管理",
      children: [
        {
          id: 41,
          title: "采购管理",
          children: [
            {
              id: 411,
              title: "采购管理",
            },
            {
              id: 412,
              title: "采购预测",
            }
          ]
        },

        {
          id: 42,
          title: "供应商管理",
        },
        {
          id: 43,
          title: "招标管理",
        }
      ]
    },
  ],

根据id循环想要得到:

let new = [
    {
      id: 1,
      title: "首页",
    },
    {
      id: 2,
      title: "系统管理",
      children: [
        {
          id: 21,
          title: "个人信息",
        },
        {
          id: 22,
          title: "用户管理",
        },
      ]
    },
    {
      id: 3,
      title: "基础配置",
      children: [
        {
          id: 31,
          title: "数据字典",
        }
      ]
    },
    {
      id: 4,
      title: "业务管理",
      children: [
        {
          id: 41,
          title: "采购管理",
          children: [
            {
              id: 412,
              title: "采购预测",
            }
          ]
        },
        {
          id: 43,
          title: "招标管理",
        }
      ]
    },
  ],
阅读 2.1k
2 个回答

思路:树形结构筛选问题,一个节点要保留下来,则至少有一个后代元素中满足保留条件,或者它本身满足保留条件,这里的保留条件即节点id是否出现在给定数组中。

给个通用的递归版本吧, func描述节点保留条件,五六行代码够简单吧:

function treeFilter(func, tree = []) {
  return tree.filter(data => {
    data.children = treeFilter(func, data.children)
    return func(data) || data.children.length
  })
}

调用:

let result = treeFilter(data => list.includes(data.id), admin)
console.log(JSON.stringify(result))

执行结果:

[
    {
        "id":1,
        "title":"首页",
        "children":[

        ]
    },
    {
        "id":2,
        "title":"系统管理",
        "children":[
            {
                "id":21,
                "title":"个人信息",
                "children":[

                ]
            },
            {
                "id":22,
                "title":"用户管理",
                "children":[

                ]
            }
        ]
    },
    {
        "id":3,
        "title":"基础配置",
        "children":[
            {
                "id":31,
                "title":"数据字典",
                "children":[

                ]
            }
        ]
    },
    {
        "id":4,
        "title":"业务管理",
        "children":[
            {
                "id":41,
                "title":"采购管理",
                "children":[
                    {
                        "id":412,
                        "title":"采购预测",
                        "children":[

                        ]
                    }
                ]
            },
            {
                "id":43,
                "title":"招标管理",
                "children":[

                ]
            }
        ]
    }
]
function find(ary = [], ids = []) {
  const result = [];
  ary.forEach(item => {
    const children = find(item.children, ids);
    if (ids.includes(item.id) || children.length) {
      const _item = {...item};
      if (children.length) {
        _item.children = children;
      } else {
        delete _item.children;
      }
      result.push(_item);
    }
  });
  return result;
}
const result = find(admin, list)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题