Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda