简体   繁体   English

使用Angular2在JSON中获取嵌套的Childrens

[英]Get nested Childrens in JSON using Angular2

I'm working on json Data with inner childrens. 我正在与内部孩子一起处理json数据。 i need to get all the name's in the json. 我需要在json中获取所有名称。

json data json资料

this.res = [  
                  {
                    "children": [
                      {
                        "children": [
                          {
                            "children": [],
                            "name": "P Sub Child 3",
                          },
                          {
                            "children": [
                              {
                                "children":[],
                                "name" : "data"
                              }
                            ],
                            "name": "PSubChild2",
                          }
                        ],
                      "name": "PChild1",
                      },
                      {
                        "children": [
                          {
                            "children": [],
                            "name": "PSubChild3",
                          }
                        ],
                        "name": "PChild2",
                      }
                    ],
                    "name": "Parent1",
                  },
                  {
                  "children": [],
                  "name": "Parent2",
                  }
    ];

in my application names of every child has to store in the variables using angular2. 在我的应用程序中,每个孩子的名字都必须使用angular2存储在变量中。 Data is dynamic changes as per the user in my application 数据是根据我的应用程序中的用户动态变化的

Here is a plunkr of a possible implementation https://plnkr.co/edit/njM1HLx7vuZY9loto2pM?p=preview 这是一个可能实现的插件https://plnkr.co/edit/njM1HLx7vuZY9loto2pM?p=preview

Create your own datatype: 创建自己的数据类型:

export class MyNode {
  children:Array<MyNode>;
  name:string;
}

and then create a method to recursively loop your parents to get their names and the names of the children 然后创建一种方法来递归循环您的父母以获取他们的名字和孩子的名字

parents:Array<MyNode> = [
    {
      "children": [
        {
          "children": [
            {
              "children": [],
              "name": "P Sub Child 3",
            },
            {
              "children": [
                {
                  "children":[],
                  "name" : "data"
                }
              ],
              "name": "PSubChild2",
            }
          ],
          "name": "PChild1",
        },
        {
          "children": [
            {
              "children": [],
              "name": "PSubChild3",
            }
          ],
          "name": "PChild2",
        }
      ],
      "name": "Parent1",
    },
    {
      "children": [],
      "name": "Parent2",
    }
  ];

 names:Array<string>=[];
 getAllNames(){
    this.getNameHelper(this.parents);
 }
 getNameHelper(parents:Array<MyNode>){
    for(var p of parents){
      if(p.name){
        this.names.push(p.name);
      }
      if(p.children){
        this.getNameHelper(p.children);
      }
    }
  }

From what I understand you want to just get the value of the name property down the hierarchical data you have then as most here suggested you have to use recursion to get to the name property. 据我了解,您只想从层次数据中获取name属性的值,然后像大多数此处建议的那样,您必须使用递归来获取name属性。 With Rxjs it becomes a bit easier to do like following: 使用Rxjs可以更容易地执行以下操作:

Observable.from(this.res)
.expand(d => d.children.length > 0 ? Observable.from(d.children) : Observable.empty())
.subscribe(d => console.log(d.name));

I have made a working sample here: https://jsfiddle.net/ibrahimislam/ba17w8xz/1/ 我在这里做了一个工作示例: https : //jsfiddle.net/ibrahimislam/ba17w8xz/1/

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

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