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

126
Views
Building a data structure to loop through nested children

Thanks for taking the time to read, I'm trying to work out a data structure that would be used for navigation in a react app, I do have control over the data to loop through. The potential amount of children is unknown, I can't find a good way to loop through all of the data in one function, if you could point me in the direction I'm sure I can learn from it or any help would be great! Also if would be easier to add items to the data structure that is an option!

const  data = {
  "nav": {
    "Nav Menu Title 1" : {
      "children": [
        {
          "name": "Sub Menu Title 1",
          "children" : [
            {
              "name": "Sub Sub Menu Title 1",
               "children" : [
                 {
                    "name": "Sub Sub Menu Option 1"

                  }
               ]
            },
            {
               "name" : "Sub Menu Option 1"
            }
          ]
        },
        {
          "name": "Option 1"
        },
        {
          "name": "Option 2"
        },
        {
          "name": "Option 3"
        },
        {
          "name": "Option 4"
        },
        {
          "name": "Option 5"
        }
      ]
    }
  }
}

for (let items in data.nav) {  
  let newNavItem = {
    id: 'apps.' + items,
    title: items,
    type: 'item',
    icon: 'heroicons-outline:shopping-cart'
  }
  
  if (data.nav[items].children.length > 0) {
    newNavItem.type = "collapse";
    newNavItem.children = [];
    // this is where I have trouble looping through children to add in a newNavItem
  }
}

about 4 years ago · Juan Pablo Isaza
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!