小编ewo*_*ool的帖子

javascript找到深层嵌套的对象

我需要使用javascript在深层嵌套的对象数组中递归过滤对象,也许在lodash的帮助下.最干净的方法是什么,如果我不知道我的数组中有多少嵌套对象?

假设我有以下结构

[
  {
    label: "first",
    id: 1,
    children: []
  },
  {
    label: "second",
    id: 2,
    children: [
      {
        label: "third",
        id: 3,
        children: [
          {
            label: "fifth",
            id: 5,
            children: []
          },
          {
            label: "sixth",
            id: 6,
            children: [
              {
                label: "seventh",
                id: 7,
                children: []
              }
            ]
          }
        ]
      },
      {
        label: "fourth",
        id: 4,
        children: []
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

我想找到一个id 6,如果它有子,则返回true,否则为false.

当然,如果我有一个类似的数据结构但具有不同数量的项目,它也应该工作.

javascript lodash

2
推荐指数
1
解决办法
438
查看次数

标签 统计

javascript ×1

lodash ×1