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

209
Views
iterating through nested array of objects in javascript

I have the following data that i need to iterate over.

data structure

I have came up with the following raw solution to loop through and get my needed data, not sure if this is the most optimal way of doing so, is there any way to speed this up or just to improve the solution?

 Object.entries(data[0]).forEach(([key, value]) => {
  for (const [key, test] of Object.entries(value)) {
    for (const [key, properties] of Object.entries(test.properties)) {
      for (const [keys, prop] of Object.entries(properties)) {
         console.log(prop);
      }
    }
    for (const [bestkeys, provisions] of Object.entries(test.provisions)) {
         //console.log(provisions);
    }
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If there is a specific pattern that you need to follow, then explicitly stating your pattern is usually the best way to go, especially for a less complex pattern such as this. I re-wrote your example but using .values instead of .entries because you only used the values.

Object.values(data[0]).forEach(val => {
  Object.values(val).forEach(test => {
    Object.values(test).forEach(property => {
      Object.values(property).forEach(prop => {
        console.log(prop);
      });
    });
    Object.values(test.provisions).forEach(provisions => {
      console.log(provisions);
    });
  });
});

However, if you only need to traverse all the branches and log all the deepest level of values, you could use recursive logic as well. i.e.

function logDeepestNestedValue(obj) {
  if (typeof obj === "object") {
    const values = Object.values(obj);
    values.forEach(logDeepestNestedValue);
  } else {
    console.log(obj);
  }
}
logDeepestNestedValues(data[0]);

Looking at your image I noticed that test.provisions might be an array and not an object, in which case you would not want to call Object.values on it, instead you can just use .forEach or directly a for of loop to log things there.

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!