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

86
Views
How to continuesly loop inside child elements of child in an object

Is there any best option that i could use to dig, into the deep of objects, beside make some static code like this?

for(let u in menu)
  for(let v in menu.list[u])
    for(let w in menu.list[u].list[v])
      for(let x in menu.list[u].list[v].list[w])
        for(let y in menu.list[u].list[v].list[w].list[x])
          for(let z in menu.list[u].list[v].list[w].lisit[x].list[y])
            console.log(menu.list[u].list[v].list[w].lisit[x].list[y][z])

i couldn find the correct algorithm for this, the object is a bit random, so its probably will be a waste of if the object only have two or even on list element.

the object look like this

 list: {
   somethingA: {
      list: {
        somethingAA: {
          list: {}
        },
        somethingAB: {
          list: {
            somethingABA: {}
            somethingABB: {
               list: {
                 somethingABBA: {}
               }
            }
          }
        }
      }
   },
   somethingB: {
      list: {
        somethingBA: {
          list: {}
        },
        somethingBB: {
          list: {}
        }
      }
   }
 }

i could put break, if the last list element didnt has list element, but how if the list has child list, more than the static loop i made? which i need to be visible.

thanks

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

0

You could take a recursive approach and iterate only list properties.

const
    visitList = object => {
        if ('list' in object) {
            for (const key in object.list) visitList(object.list[key]);
        } else {
            console.log(object);
        }
    },
    data = { list: { somethingA: { list: { somethingAA: { list: {} }, somethingAB: { list: { somethingABA: {}, somethingABB: { list: { somethingABBA: {} } } } } } }, somethingB: { list: { somethingBA: { list: {} }, somethingBB: { list: {} } } } } };

visitList(data);

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!