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

120
Views
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 answers
Answer question

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