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

121
Vistas
Dealing with looping through nested arrays

I have data array, which has nested arrays inside (level1arr, leve21arr ...)

const data = [
  {
    level1arr: [
      {
        level2arr: [{ id: 1, isValid: true }, { id: 2, isValid: true }, { id: 3, isValid: true }],
      },
      {
        level2arr: [{ id: 4, isValid: true }, { id: 5, isValid: true }, { id: 6, isValid: true }],
      },
    ],
  },
  {
    level1arr: [
      {
        level2arr: [{ id: 7, isValid: true }, { id: 8, isValid: true }, { id: 9, isValid: true }],
      },
      {
        level2arr: [{ id: 10, isValid: true }, { id: 11, isValid: true }, { id: 12, isValid: true }],
      },
    ],
  },
];

I also have another array:

const invalidIds = [2,5]

I want to find elements with apecyfic id and change isValid property to false. Is it better way than iteratinf over multiple nested arrays, like that:

data.forEach(lvl1 => {
  lvl1.level1arr.forEach(lvl2 => {
    lvl2.level2arr.forEach(element => {
      // further nesting
    });
  });
}) 

Such iterating over multiple arrays is not good for performance. What is the best way to handle such case with nested arrays?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If it were nested arrays, you could use Array.prototype.flat(). However, you have a mix of nested objects and arrays. You will have to write a custom "flattener" for this data structure. Check this answer for details: how to convert this nested object into a flat object?

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use recursion until you reach the level you need. Here's one way to do it.

const data = [{
    level1arr: [{
        level2arr: [{
          id: 1,
          isValid: true
        }, {
          id: 2,
          isValid: true
        }, {
          id: 3,
          isValid: true
        }],
      },
      {
        level2arr: [{
          id: 4,
          isValid: true
        }, {
          id: 5,
          isValid: true
        }, {
          id: 6,
          isValid: true
        }],
      },
    ],
  },
  {
    level1arr: [{
        level2arr: [{
          id: 7,
          isValid: true
        }, {
          id: 8,
          isValid: true
        }, {
          id: 9,
          isValid: true
        }],
      },
      {
        level2arr: [{
          id: 10,
          isValid: true
        }, {
          id: 11,
          isValid: true
        }, {
          id: 12,
          isValid: true
        }],
      },
    ],
  },
];

const invalidIds =[2,5]

const findId = (object, key, value) => {
  if (Array.isArray(object)) {
    for (const obj of object) {
      findId(obj, key, value);
    }
  } else {
    if (object.hasOwnProperty(key) && object[key] === value) {
      object.isValid = false;
      return object
    }
    for (const k of Object.keys(object)) {
      if (typeof object[k] === "object") {
        findId(object[k], key, value);
      }
    }
  }
}

invalidIds.forEach(id => findId(data, "id", id))
console.log(data)

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