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:

["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

Code Maniac :

You can use a recursive 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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get all values from an array of objects with nested arrays of objects?

How to get the combination of array values from nested arrays in an array of objects

Loop through array of arrays which contain objects and get all specific values from the objects then show to the page JQuery

How to get objects with unique values for specific keys from array of objects?

How to find all values of a specific key in an array of nested objects?

How to get field and values from the nested document and ignore arrays and objects

How to get array of unique values from arrays within an array of objects?

How get to All values matching the query from mongodoDb at Once from multiple documents having nested array of objects

How to destructure all of the objects from the nested arrays?

How to get all values of a specific key in array of objects?

How to get all keys with values from nested objects

How to get all used values from all objects in array in ejs

JavaScript - How to put all of the objects from several nested arrays into a new array of unique objects

How to get & display specific values from array of objects in MySQL DB?

How to get values from specific objects inside an array?

How to get values from json nested objects

How to get an array of the values of all properties of objects inside an array of objects?

How to create an array of objects with all objects from multiple arrays?

How to get all values of objects inside array

Build an array of nested objects from arrays of properties names and values

How to get the value from a nested array with objects

Get all keys in array of objects whose values are arrays

Extract all specific properties from a nested array of objects

How to get values from one array of objects into another array of objects

How to flatten nested arrays of objects into an array of arrays?

How to get values from array of objects in javascript

Reactjs: How to get values from array of objects

How to check a json schema for specific values in a objects of a nested array?

Get an array from nested objects