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

104
Visualizações
How to iterate recursively over all children in nested objects

I am trying to iterate over all objects in my array and all children and for each I want to set the folded property to false

But I am getting an error:

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

Here is my array:

[
    {
        "id": 1,
        "title": "Title",
        "folded": true,
        "children": []
    },

    {
        "id": 2,
        "title": "Title",
        "folded": true,
        "children": [
            {
                "id": 3,
                "title": "Title",
                "folded": true,
                "children": []
            },

            {
                "id": 4,
                "title": "Title",
                "folded": true,
                "children": [
                    {
                        "id": 6,
                        "title": "Title",
                        "folded": true,
                        "children": []
                    }
                ]
            }
        ]
    },

    {
        "id": 5,
        "title": "Title",
        "folded": true,
        "children": []
    }
]

And here is my function

function selectActivePage(node) {
    for (let child of node.children) {
        child.$folded = false
        selectActivePage(child)
    }
}

selectActivePage(myArray)
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are passing child which is an object and that is not iterable, you have to pass it's children. You can try checking if child having children array and then iterate children.

function selectActivePage(node) {
    for (let child of node) {
        child.folded = false;
        if(child.children && Array.isArray(child.children) && child.children.length > 0)
            selectActivePage(child.children)
    }
};
about 4 years ago · Santiago Trujillo Relatório
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