简体   繁体   English

如何获得 JSON 数据的最内层孩子

[英]how to get inner most child of a JSON data

I have a JSON data which is something like this, And my requirement to fetch the data of inner most children ie hierarchyLevel: 4 .我有一个类似这样的 JSON 数据,我需要获取最内部孩子的数据,即hierarchyLevel: 4 And this JSON data is not static, the hierarchyLevel can go any thing like 5, 6, 7 any thing.而且这个 JSON 数据不是静态的,hierarchyLevel 可以是任何东西,比如 5、6、7 任何东西。 Please help to find solution in javascript.请帮助在javascript中找到解决方案。

{
  "hierarchylist": [
    {
      "hierarchyId": 10,
      "hierarchyLevel": 0,
      "name": "ABC",
      "parentId": 0,
      "children": [
        {
          "hierarchyId": 12,
          "hierarchyLevel": 1,
          "name": "ABC-Child1",
          "parentId": 10,
          "children": [
            {
              "hierarchyId": 2,
              "hierarchyLevel": 2,
              "name": "People Management & Development1 ",
              "parentId": 12,
              "children": [
                {
                  "hierarchyId": 5,
                  "hierarchyLevel": 3,
                  "name": "Resourcing2_1",
                  "parentId": 2,
                  "children": [
                    {
                      "hierarchyId": 19,
                      "hierarchyLevel": 4,
                      "name": "Resource Request ",
                      "parentId": 5,
                      "children": [],
                      "docId": 19,
                      "docstatusid": 20
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Thanks.谢谢。

This should get you the inner-most one:这应该让你得到最内在的:

 let data = { "hierarchylist": [ { "hierarchyId": 10, "hierarchyLevel": 0, "name": "ABC", "parentId": 0, "children": [ { "hierarchyId": 12, "hierarchyLevel": 1, "name": "ABC-Child1", "parentId": 10, "children": [ { "hierarchyId": 2, "hierarchyLevel": 2, "name": "People Management & Development1 ", "parentId": 12, "children": [ { "hierarchyId": 5, "hierarchyLevel": 3, "name": "Resourcing2_1", "parentId": 2, "children": [ { "hierarchyId": 19, "hierarchyLevel": 4, "name": "Resource Request ", "parentId": 5, "children": [], "docId": 19, "docstatusid": 20 } ] } ] } ] } ] } ] } let children = data.hierarchylist[0].children; while(children[0] && children[0].children && children[0].children.length) { children = children[0].children; } console.log(children);

You can try the while loop, here's the example你可以试试while循环,这里是例子

var children = json['hierarchylist']['children'][0];

while(typeof children !== 'undefined') {
    children = children['children'][0];
}

console.log(children);

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

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