Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda