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

100
Views
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 answers
Answer question

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 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!