Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Obtain values fron nested array of objects

I have the fallowing data structure:

products = [
    {
        name: 'Menu1',
            subitems: [
                {
                    name: 'Menu2'
                },
        ]
    },
    {
        name: 'Menu3',
            subitems: [
                {
                    name: 'Menu4',
                    subitems: [
                        {
                            name:'Menu5'
                        },
                        {
                            name:'Menu6'
                        }
                    ]
                },
        ]
    },
]

Want to obtain all the names values inside of it, no matter how deep nested it is. I 've tried solutions related to flat, reduce, concat, map, for of, etc but I never get the deepest level 'Menu5' and 'Menu6'

I think this is saying recursivity out loud but I can't implement it

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using Array#reduce:

const _getNames = (products = []) =>
  products.reduce((names, { name, subitems = [] }) => {
    names.push(name);
    if(subitems.length > 0) {
      names.push(..._getNames(subitems));
    }
    return names;
  }, []);

const products = [
  { 
    name: 'Menu1',
    subitems: [ 
      { name: 'Menu2' } 
    ] 
  },
  { 
    name: 'Menu3', 
    subitems: [ 
      { name: 'Menu4', 
        subitems: [ 
          { name:'Menu5' }, { name:'Menu6' } 
        ] 
      }
    ]
  }
];
console.log( _getNames(products) );

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!