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

117
Vistas
Optimizing Double For-Each, Or At Least Making It More Readable?

So right now I have an object with the following shape and the task is to loop over every item in all of the arrays and compare each one to all of the other items in all of the arrays.

things: Things = {
    "Type1Things": [],
    "Type2Things": [],
    "Type3Things": [],
}

Here's how I currently have it written:

Object.keys(things).forEach( (key: string) => {
    things[key].forEach( ( outerThing: Overlay ) => {

        Object.keys(things).forEach( (key1: string) => {
            things[key1].forEach( ( innerThing: Overlay ) => {

                // compare inner and outer things
            })
        })
    })
})

Is there a better method to perform this task, or at least a more readable way of writing it? Could a recursive function better accomplish this?

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

0

I've abstracted this out using Typescript. Might not be exactly what you're looking for but it'll help with readability, and errors that popup when grabbing the keys from objects. Also saves you having to write most of the typings in your example.

const OBJ = {
  Keys: <T extends Record<string, unknown>>(obj: T): (keyof T)[] =>
    (Object.keys(obj) as any)
}

Usage:

for (const keyX in OBJ.Keys(things)) {
  for (const keyY in OBJ.Keys(things[keyX])) {
    console.log(`${keyY}: ${things[keyX][keyY]}`);
}
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