简体   繁体   English

如何从具有嵌套 arrays 对象的对象数组中获取所有特定值?

[英]How to get all specific values from an array of objects with nested arrays of objects?

I have an array:我有一个数组:

   const arr = [
  {
    name: "name 1",
    dontShow: true,
    children: [
      {
        name: "name 2",
        key4: 4,
        dontShow: false,
        children: [],
      },
    ],
  },
  {
    name: "name 3",
    dontShow: false,
    children: [
      {
        name: "name 4",
        dontShow: true,
        children: [
          {
            name: "name 5",
            dontShow: false,
            children: null,
          },
        ],
      },
    ],
  },
];

I need an array of names from every object, except those that have property dontShow: true So from that example I would expect such array:我需要来自每个 object 的名称数组,但具有属性dontShow: true的名称除外 所以从那个例子中我希望这样的数组:

["name2", "name3", "name5"]

Basically, I need to get a flat array from tree-like structure, lodash/underscore solutions would be also great, I just didn't find them基本上,我需要从树状结构中获得一个平面数组,lodash/underscore 解决方案也很棒,我只是没有找到它们

You can use a recursive function您可以使用递归 function

 const arr = [{ name: "name 1", dontShow: true, children: [{ name:"name 2", key4: 4, dontShow: false, children: [], }, ],},{name: "name 3",dontShow: false,children: [{ name: "name 4", dontShow: true, children: [{ name: "name 5", dontShow: false, children: null,},],}, ],},]; let final = (arr, result = []) => { if (Array.isArray(arr)) { arr.forEach(obj => { if (.obj.dontShow) { result.push(obj.name) } if (Array.isArray(obj.children)) { final(obj,children. result) } }) } return result } console.log(final(arr))

You could get a flat array of names with a look to dontShow .您可以通过查看dontShow获得一组平面名称。

 const getNames = array => array.flatMap(({ name, dontShow, children }) => [...(dontShow? []: [name]), ...getNames(children || []) ]), array = [{ name: "name 1", dontShow: true, children: [{ name: "name 2", key4: 4, dontShow: false, children: [] }] }, { name: "name 3", dontShow: false, children: [{ name: "name 4", dontShow: true, children: [{ name: "name 5", dontShow: false, children: null, }] }] }], result = getNames(array); console.log(result);

暂无
暂无

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

相关问题 如何从对象数组中的嵌套数组中获取数组值的组合 - How to get the combination of array values from nested arrays in an array of objects 遍历包含对象的 arrays 数组并从对象中获取所有特定值,然后显示到页面 JQuery - Loop through array of arrays which contain objects and get all specific values from the objects then show to the page JQuery 如何在 React 中将嵌套数组中的所有对象提取到一个数组中 - how to extract all objects from nested arrays into one array in React 如何在嵌套对象数组中查找特定键的所有值? - How to find all values of a specific key in an array of nested objects? 如何从对象数组中获取具有特定键的唯一值的对象? - How to get objects with unique values for specific keys from array of objects? 如何一次从具有嵌套对象数组的多个文档中获取与来自 mongodoDb 的查询匹配的所有值 - How get to All values matching the query from mongodoDb at Once from multiple documents having nested array of objects 如何获取对象数组中特定键的所有值? - How to get all values of a specific key in array of objects? JavaScript-如何将所有嵌套数组中的所有对象放入唯一对象的新数组中 - JavaScript - How to put all of the objects from several nested arrays into a new array of unique objects 如何从嵌套对象中获取所有具有值的键 - How to get all keys with values from nested objects 如何从非常复杂的深度嵌套的对象和数组数组中取出对象? - How to get an objects out from very complex deeply nested array of objects and arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM