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

185
Views
create a nested array recursively in angular-10

Following is my array - Name (categoriesData)

[{"categoryCode":"cat1","name":"Procurement","parentCategoryCode":"ROOT_NODE","active":true,"archive":false,"children":[]}]

There is a object and it looks like - Name (categoryData)

{"8596":{"operation":true,"children":[]},"9890":{"operation":true,"children":[{"categoryCode":"8596","name":"Sub-Sub-Sub-Procurement","parentCategoryCode":"9890","active":true,"archive":false}]},"125240":{"operation":true,"children":[{"categoryCode":"9890","name":"Syb-Sub-Procurement","parentCategoryCode":"125240","active":true,"archive":false}]},"dvs":{"operation":true,"children":[]},"cat1":{"operation":true,"children":[{"categoryCode":"125240","name":"Procurement-Sub","parentCategoryCode":"cat1","active":true,"archive":false}]}}

In categoriesData, the object has children and is empty.

But this will be recursive, the categoryCode is cat1 and cat1 has children in categoryData and in that children of cat1 in categoryData has categoryCode 125240 and 125240 has children in categoryData and this will go on till the time the categoryData children for that specific category code is empty.

Can any one help?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

your categoriesData array data as (this.categoryCode and final = {}):

[{"categoryCode":"cat1","name":"Procurement","parentCategoryCode":"ROOT_NODE","active":true,"archive":false,"children":[]}]

your categoryData as (this.value1) :

{"8596":{"operation":true,"children":[]},"9890":{"operation":true,"children":[{"categoryCode":"8596","name":"Sub-Sub-Sub-Procurement","parentCategoryCode":"9890","active":true,"archive":false}]},"125240":{"operation":true,"children":[{"categoryCode":"9890","name":"Syb-Sub-Procurement","parentCategoryCode":"125240","active":true,"archive":false}]},"dvs":{"operation":true,"children":[]},"cat1":{"operation":true,"children":[{"categoryCode":"125240","name":"Procurement-Sub","parentCategoryCode":"cat1","active":true,"archive":false}]}}

Typescript code

  utils(){
for(let  i= 0 ; i< this.categoriesData.length; i++){
  this.help(this.categoriesData[i]['categoryCode']);
  this.categoriesData[i]['children'] = this.final['children'];
}
console.log(this.categoriesData)

}

help(code) {

if (!this.categoryData[code].children.length) {
  this.final = this.categoryData[code]['children'];
  return this.categoryData[code];
}
this.help(this.categoryData[code]['children'][0]['categoryCode']);
this.final = { ['children']: [{ ...this.categoryData[code]['children'][0], ...this.final }] }

}

your final output

[
{
    "categoryCode": "cat1",
    "name": "Procurement",
    "parentCategoryCode": "ROOT_NODE",
    "active": true,
    "archive": false,
    "children": [
        {
            "categoryCode": "125240",
            "name": "Procurement-Sub",
            "parentCategoryCode": "cat1",
            "active": true,
            "archive": false,
            "children": [
                {
                    "categoryCode": "9890",
                    "name": "Syb-Sub-Procurement",
                    "parentCategoryCode": "125240",
                    "active": true,
                    "archive": false,
                    "children": [
                        {
                            "categoryCode": "8596",
                            "name": "Sub-Sub-Sub-Procurement",
                            "parentCategoryCode": "9890",
                            "active": true,
                            "archive": false
                        }
                    ]
                }
            ]
        }
    ]
}

]

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!