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

118
Views
Access property to the infinity

I have the following form object:

{
    "name":"form name",
    "forms":[
        {
            "name":"form 1",
            "forms":[
                ...
            ]
        }
    ]
}

Forms can contain forms inside them.

The idea was to print all the form names.

I did the following:

forms.forEach(form -> {
  console.log(form.name);
  
  form.forms.forEach(f -> {
     console.log(f.name);

     f.forms.forEach(...);
  })
});

I have no idea how many forms can be inside so how can I do this to infinity.

Thanks

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

0

Use recursion

const pr = (data) => {
  console.log(data.name);
  data.forms.forEach((f) => pr(f));
};
about 4 years ago · Juan Pablo Isaza Report

0

You should use recursion. Could be something like this:

function logFormName(data) {
  if (data.name) {
    console.log(data.name)
  }
  if (data.forms) {
    return data.forms.forEach((form) => logFormName(form))
  }
  return;
}
about 4 years ago · Juan Pablo Isaza Report

0

// forms data
let forms = {
  "name":"form name",
  "forms":[
      {
          "name":"form 1",
          "forms":[
              
          ]
      }
  ]
}


// recursive function to iterate through each form
// and print name of the form
function printForms(forms) {

if(forms.hasOwnProperty("forms")){
  console.log(forms.name);
  
  // iterate over each form        
  forms.forms.forEach(form => {
  
    // recursive call           
    printForms(form);
  })
}
}

// call function to print name of each form
printForms(forms)
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!