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

121
Views
How do i loop through an array and restructure it to be deeply nested json for vue plugin

I need to reformat json with a query into a spesific nested format

[
  {
    "id": 54,
    "parent_id": null,
  },
  {
    "id": 55,
    "parent_id": 54,
  },
  {
    "id": 56,
    "parent_id": 54,
  },
  {
    "id": 57,
    "parent_id": 55,
  },
  {
    "id": 58,
    "parent_id": 55,
  },
  {
    "id": 59,
    "parent_id": 55,
  }
]

Im looking for a js algorithm to solve this problem //The above json needs to be in this format: //(not accurate acourding to given data)

[
  { 
   "id": 54,
   "children": [
       { "id": 55, "children": [] },
       { "id": 53, "children": [
         { "id": 50, "children": [] }
       ]
      }
    ]
  }
]

I have this script that works but its very in efficient to loop through every instance and child........................................................................................

createDataSet(){
            let array = []
            let data = this.getData()

            data.forEach(level => {
              if(level.parent_id === null){
                array.push({id: level.id, name: level.name, title: level.name, children: []})
              }
            });
            array.forEach(element => {
              data.forEach(level => {
                if(level.parent_id === element.id){
                  element.children.push(
                    {id: level.id, name: level.name, title: level.name, children: []}
                  )
                }
              });
            });
            array.forEach(element => {
              element.children.forEach(child => {
                data.forEach(level => {
                  if(level.parent_id === child.id){
                    child.children.push(
                      {id: level.id, name: level.name, title: level.name, children: []}
                    )
                  }
                })
              })
            })
            array.forEach(element => {
              element.children.forEach(child => {
                child.children.forEach(granChild => {
                  data.forEach(level => {
                    if(level.parent_id === granChild.id){
                      granChild.children.push(
                        {id: level.id, name: level.name, title: level.name, children: []}
                      )
                    }
                  })
                })
              })
            })
return array
}
almost 4 years ago · Santiago Trujillo
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!