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

104
Views
Javascript nested object recursive

So ,i did my research and so far i cannot find any good solution to my problem...

so i have an object approach so i have tried a recursive function but it doesnt work at all (why i am here)

this function go thru one layer and stops she finds that id 3 , id 11 have both 1 childrens , while i want it to go deeper

i hope it is not a duplicate post

and i thank you in advance

#function

const haveChild = (obj, parent_id=0) =>{
        for (const [key,value] of Object.entries(obj)){
            if (value && typeof value === "object"){
                if (value['childrens']){
                    for (const [k,v] of Object.entries(value.childrens)){
                        haveChild(v,v.parent_id)
                        console.log('-----',v.name,'parent id= ',v.parent_id)

                    }
                }else{

                }
            }

        }

#data from API


{ 
    "2": {
      "id": "2",
      "parent_id": "0",
      "name": "name1"
    },
    "3": {
      "id": "3",
      "parent_id": "0",
      "name": "name2",
      "childrens": {
        "9": {
          "id": "9",
          "parent_id": "3",
          "name": "name3"
        }
      }
    },
    "11":{
      "id": "11",
      "parent_id": "0",
      "name": "name4",
      "childrens": {
        "16": {
          "id": "16",
          "parent_id": "11",
          "name": "name5",
          "childrens": {
            "21": {
              "id": "21",
              "parent_id": "16",
              "name": "name6",
              "childrens": {
                "23": {
                  "id": "23",
                  "parent_id": "21",
                  "name": "name7"
                }
              }
            }
          }
        }
      }
    } 
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

the problem is that the first time you call the function, in the for loop, key is the key of every object and value the corresponding value

But from the second time key is the name of the attribute and value the value of the attribute So you have to create another object in this way:

{[k]: v}

I also move the console.log outside the inner for loop

const haveChild = (obj, parent_id=0) =>{
        for (const [key,value] of Object.entries(obj)){
            if (value && typeof value === "object"){
                if (value.childrens){
                    for (const [k,v] of Object.entries(value.childrens)){
                        haveChild({[k]: v},v.parent_id)
                    }
                }
                console.log('-----',value.name,'parent id= ',value.parent_id);
            }
       }
}
about 4 years ago · Juan Pablo Isaza 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!