Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda