javascript - 在深层嵌套对象中按特定键查找对象 | JavaScript

标签 javascript arrays function object ecmascript-6

如果我不知道我的对象中有多少个嵌套对象,那么根据 id 查找对象并返回该对象的最干净方法是什么?

假设我有以下结构:

  myObj = {
              "id": "5e6b8961ba08180001a10bb6",
              "children": [
                {
                  "id": "5e6b8961ba08180001a10bb7",
                  "refrenceId": "SEC-02986",
                  "children": [
                    {
                      "id": "5e58d7bc1bbc71000118c0dc"
                    },
                    {
                      "id": "5e58d7bc1bbc71000118c0dd",
                      "refrenceId": "SKU-00343"
                     },
                    {
                      "id": "5e590d571bbc71000118c102",
                      "refrenceId": "SKU-05290"
                    },
                    {
                      "id": "5e590df71bbc71000118c109",
                      "children": [
                        {
                          "id": "5e590df71bbc71000118c10a"
                        },
                        {
                          "id": "5e590df71bbc71000118c10b",
                          "refrenceId": "SKU-00444"
                    },
                    {
                      "id": "5e5cb9428ae591000177c0f6"
                    }
                  ]
                },
                {
                  "id": "5e81899f0bab450001dcfc1d",
                  "refrenceId": "SEC-03260"
                },
                {
                  "id": "5e81c4b51503860001f97f6c",
                  "refrenceId": "SEC-03267",
                  "children": [
                    {
                      "id": "5e8ad5175d374200014edb3a",
                      "refrenceId": "SEC-03409",
                      "children": [
                        {
                          "id": "5e8f28882d94c1000156bebe"
                        }
                      ]
                    },
                    {
                      "id": "5e8ad5175d374200014edb3c",
                       "refrenceId": "SEC-03410"
                    },
                    {
                      "id": "5e8f29082d94c1000156bec6",
                      "refrenceId": "SEC-03495"
                    }
                  ]
                }
              ]
            }

假设,我想找到 id 为“5e590df71bbc71000118c10b”的对象,并从嵌套对象中返回该对象。

我尝试使用以下代码:

   function nodeHasChildren(children, id) {

    for (const child of children) {

      if (child.id === id) {

        if (Array.isArray(child.children) && child.children.length > 0) {
          return child;
        }
      }
      else {
          const result = nodeHasChildren(child.children, id);

        if (result !== undefined) {
          return result
        }
      }
    }
  }

console.log(nodeWithIdHasChildren(myObj, "5e590df71bbc71000118c10b"));

最佳答案

使用简单的递归

function findDeepById(node, id) {
  if (node.id === id) return node;
  if (node.children) {
    for(const child of node.children){
      const match = findDeepById(child, id);
      if (match) return match;
    }
  }
}
<小时/>

const myObj = {
  "id": "5e6b8961ba08180001a10bb6",
  "children": [{
    "id": "5e6b8961ba08180001a10bb7",
    "refrenceId": "SEC-02986",
    "children": [{
        "id": "5e58d7bc1bbc71000118c0dc"
      },
      {
        "id": "5e58d7bc1bbc71000118c0dd",
        "refrenceId": "SKU-00343"
      },
      {
        "id": "5e590d571bbc71000118c102",
        "refrenceId": "SKU-05290"
      },
      {
        "id": "5e590df71bbc71000118c109",
        "children": [{
            "id": "5e590df71bbc71000118c10a"
          },
          {
            "id": "5e590df71bbc71000118c10b",
            "refrenceId": "SKU-00444"
          },
          {
            "id": "5e5cb9428ae591000177c0f6"
          }
        ]
      },
      {
        "id": "5e81899f0bab450001dcfc1d",
        "refrenceId": "SEC-03260"
      },
      {
        "id": "5e81c4b51503860001f97f6c",
        "refrenceId": "SEC-03267",
        "children": [{
            "id": "5e8ad5175d374200014edb3a",
            "refrenceId": "SEC-03409",
            "children": [{
              "id": "5e8f28882d94c1000156bebe"
            }]
          },
          {
            "id": "5e8ad5175d374200014edb3c",
            "refrenceId": "SEC-03410"
          },
          {
            "id": "5e8f29082d94c1000156bec6",
            "refrenceId": "SEC-03495"
          }
        ]
      }
    ]
  }]
};

function findDeepById(node, id) {

  if (node.id === id) return node;
  if (node.children) {
    for(const child of node.children){
      const match = findDeepById(child, id);
      if (match) return match;
    }
  }
}

console.log(findDeepById(myObj, "5e590df71bbc71000118c10b"));

关于javascript - 在深层嵌套对象中按特定键查找对象 | JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61231373/

相关文章:

javascript - 在开发过程中自动连续运行 Jasmine 测试

c - 在 C 中使用 scanf 错误输入二维数组

javascript - 在javascript中声明和定义函数

c - 如何在 c 中的未知类型数组中查找最大元素(使用指向函数的指针)

python - 使用IF处理Python错误

javascript - 如何引用被 knockout 组件更改的元素?

javascript - 从浏览器启动 dialer.exe?

PHP 字符串到 javascript 可能会破坏代码 - 我猜是某个字符有问题吗?

arrays - 查找数组字符串中的第一个字符

arrays - 从 Json Angular 5 插入数组