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

111
Views
¿Optimizar Double For-Each, o al menos hacerlo más legible?

Así que ahora mismo tengo un objeto con la siguiente forma y la tarea es recorrer cada elemento en todas las matrices y comparar cada uno con todos los demás elementos en todas las matrices.

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

Así es como lo tengo escrito actualmente:

 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 }) }) }) })

¿Existe un método mejor para realizar esta tarea, o al menos una forma más legible de escribirla? ¿Podría una función recursiva lograr esto mejor?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

He resumido esto usando Typescript. Puede que no sea exactamente lo que está buscando, pero ayudará con la legibilidad y los errores que aparecen al tomar las teclas de los objetos. También le ahorra tener que escribir la mayoría de los tipos en su ejemplo.

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

Uso:

 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 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!