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

222
Views
How can I extract keys and values from a nested object of objects?

This is a question in my last interview and I'm trying to resolve that: in this case, I want to catch values in the nested object and log them into the console.

const obj1 = {
  foo: 1,
  bar: {
    foo1: 2,
    bar1: {
      foo2: {
        foo3: 3,
        bar2: 4
      },
      bar3: 5
    }
  }
};

// output: [1,2,3,4,5] //

actually, I mean in a professional way. not with this way:

[obj1.foo, obj1.bar.foo1, obj1.bar.bar1.foo2.foo3, obj1.bar.bar1.foo2.bar2, obj1.bar.bar1.bar3]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do something like this

const obj1 = {
  foo: 1,
  bar: {
    foo1: 2,
    bar1: {
      foo2: {
        foo3: 3,
        bar2: 4
      },
      bar3: 5
    }
  }
};

const getValues = (data, values= []) => {
  if(typeof data !== 'object'){
    return [...values, data]
  }
  return Object.values(data).flatMap(v => getValues(v, values))
}

console.log(getValues(obj1))

about 4 years ago · Juan Pablo Isaza Report

0

You can get specific property like this:

> function nestedObject (nested) {
>     for (const key in nested) {
>       if (typeof nested[key] === 'object') {
>         for (const nesty in nested[key]) {
>            console.log(nested[key][nesty]);
>         }
>       } else {
>         console.log(nested[key]);
>       }
>     }   }
>      nestedObject(obj1);
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!