Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda