简体   繁体   English

通过深层嵌套数组对象中的特定键查找所有子键

[英]Find all children's key by specific key in a deep nested array object

const data = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [
          { title: '0-0-0-0', key: '0-0-0-0' },
          { title: '0-0-0-1', key: '0-0-0-1' },
          { title: '0-0-0-2', key: '0-0-0-2' },
        ],
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        children: [
          { title: '0-0-1-0', key: '0-0-1-0' },
          { title: '0-0-1-1', key: '0-0-1-1' },
          { title: '0-0-1-2', key: '0-0-1-2' },
        ],
      },
      {
        title: '0-0-2',
        key: '0-0-2',
      },
    ],
  },
  {
    title: '0-1',
    key: '0-1',
    children: [
      { title: '0-1-0-0', key: '0-1-0-0' },
      { title: '0-1-0-1', key: '0-1-0-1' },
      { title: '0-1-0-2', key: '0-1-0-2' },
    ],
  },
  {
    title: '0-2',
    key: '0-2',
  },
];

How would I get an array of all values throughout all nests of this obj by the key of id.我如何通过 id 的键在这个 obj 的所有嵌套中获取所有值的数组。

For example例如

input: ["0-0-0"]输入:[“0-0-0”]

i wanna output like this我想这样输出

output: ["0-0-0", "0-0-0-0", "0-0-0-1", "0-0-0-2"]输出:[“0-0-0”,“0-0-0-0”,“0-0-0-1”,“0-0-0-2”]

enter image description here在此处输入图像描述

You can recursively loop over all the children and get the keys that either match one of the target keys or if any of their ancestors have matched one the target keys.您可以递归地遍历所有children项并获取与目标键之一匹配的键,或者如果它们的任何祖先匹配目标键之一。

 const data = [ { title: "0-0", key: "0-0", children: [ { title: "0-0-0", key: "0-0-0", children: [ { title: "0-0-0-0", key: "0-0-0-0" }, { title: "0-0-0-1", key: "0-0-0-1" }, { title: "0-0-0-2", key: "0-0-0-2" }, ], }, { title: "0-0-1", key: "0-0-1", children: [ { title: "0-0-1-0", key: "0-0-1-0" }, { title: "0-0-1-1", key: "0-0-1-1" }, { title: "0-0-1-2", key: "0-0-1-2" }, ], }, { title: "0-0-2", key: "0-0-2", }, ], }, { title: "0-1", key: "0-1", children: [ { title: "0-1-0-0", key: "0-1-0-0" }, { title: "0-1-0-1", key: "0-1-0-1" }, { title: "0-1-0-2", key: "0-1-0-2" }, ], }, { title: "0-2", key: "0-2", }, ]; function getKeys(data, targetKeys) { const targetKeysSet = new Set(targetKeys); const outputKeys = []; function getKeysHelper(data, hasParentMatched = false) { data?.forEach((d) => { if (targetKeysSet.has(d.key) || hasParentMatched) { outputKeys.push(d.key); getKeysHelper(d.children, true); } else { getKeysHelper(d.children); } }); } getKeysHelper(data); return outputKeys; } getKeys(data, ["0-0-0"]);

Relevant documentations:相关文件:

you can do something like this你可以做这样的事情

 const extractKeys = data => { const loop = (data, res) => { if(!data.children){ return [...res, data.key] } return data.children.flatMap(d => loop(d, [...res, data.key])) } return [...new Set(loop(data, []))] } const findKeys = (data, keys) => data.flatMap(extractKeys).filter(k => keys.some(key => k.includes(key))) const data = [ { title: '0-0', key: '0-0', children: [ { title: '0-0-0', key: '0-0-0', children: [ { title: '0-0-0-0', key: '0-0-0-0' }, { title: '0-0-0-1', key: '0-0-0-1' }, { title: '0-0-0-2', key: '0-0-0-2' }, ], }, { title: '0-0-1', key: '0-0-1', children: [ { title: '0-0-1-0', key: '0-0-1-0' }, { title: '0-0-1-1', key: '0-0-1-1' }, { title: '0-0-1-2', key: '0-0-1-2' }, ], }, { title: '0-0-2', key: '0-0-2', }, ], }, { title: '0-1', key: '0-1', children: [ { title: '0-1-0-0', key: '0-1-0-0' }, { title: '0-1-0-1', key: '0-1-0-1' }, { title: '0-1-0-2', key: '0-1-0-2' }, ], }, { title: '0-2', key: '0-2', }, ]; console.log(findKeys(data, ['0-0-0']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM