简体   繁体   English

在 angular-10 中递归地创建一个嵌套数组

[英]create a nested array recursively in angular-10

Following is my array - Name (categoriesData)以下是我的数组 - 名称(categoriesData)

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

There is a object and it looks like - Name (categoryData)有一个对象,它看起来像 - 名称(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.在 categoriesData 中,对象有子对象并且是空的。

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.但这将是递归的,categoryCode 是 cat1 并且 cat1 在 categoryData 中有子代,并且在 categoryData 中 cat1 的子代中有 categoryCode 125240 和 125240 在 categoryData 中有子代,这将一直持续到该特定类别代码的 categoryData 子代为空的。

Can any one help?任何人都可以帮忙吗?

your categoriesData array data as (this.categoryCode and final = {}):您的 categoriesData 数组数据为(this.categoryCode 和 final = {}):

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

your categoryData as (this.value1) :您的 categoryData 为 (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
                        }
                    ]
                }
            ]
        }
    ]
}

] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM