繁体   English   中英

使用java脚本循环使用多个子节点的JSON对象

[英]looping through JSON object with multiple childrens using java script

我试图循环一个看起来像这样的Json对象

    [
      {
        "yang_type": "container",
        "name": "c1",
        "value": "",
        "children": [
          {
            "yang_type": "",
            "name": "type",
            "value": "Uint32",
            "children": []
          },
          {
            "yang_type": "list",
            "name": "DNS",
            "value": "",
            "children": [
              {
                "name": "type",
                "value": "String",
                "children": [],
                "yang_type": ""
              },
              {
                "yang_type": "leaf",
                "name": "ip-address",
                "value": "",
                "children": [
                  {
                    "name": "type",
                    "value": "string",
                    "children": [],
                    "yang_type": ""
                  }
                ]
              },
              {
                "yang_type": "leaf",
                "name": "Domain",
                "value": "",
                "children": [
                  {
                    "name": "type",
                    "value": "string",
                    "children": [],
                    "yang_type": ""
                  }
                ]
              }
            ]
          }
        ]
      }
    ]

我正在尝试这种逻辑,但它并没有循环通过第一个孩子。

while(m.children.length >= 1) {
    if(m.yang_type!='' && m.name!=''){
       {$log.error("parent:",m.yang_type,m.name);}
    }
    if(m.name!='' && m.value!=''){
       {$log.error("child:",m.name,m.value);}
    }
    m = m.children[m.children.length - 1];   
}

上面的代码并没有遍历所有的孩子。 我做错了什么?

你试图循环数组。 你的尝试不会这样。

您可以使用回调进行迭代,并将其用于子项的递归调用。

 function loop(a) { console.log(a.name); // process you data Array.isArray(a.children) && a.children.forEach(loop); // check and iterate children } var data = [{ "yang_type": "container", "name": "c1", "value": "", "children": [{ "yang_type": "", "name": "type", "value": "Uint32", "children": [] }, { "yang_type": "list", "name": "DNS", "value": "", "children": [{ "name": "type", "value": "String", "children": [], "yang_type": "" }, { "yang_type": "leaf", "name": "ip-address", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }, { "yang_type": "leaf", "name": "Domain", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }] }] }]; data.forEach(loop); 

使用缩进输出进行编辑。

 function loop(level) { return function (a) { var i = level, s = ''; while (i--) { s += ' '; } if (level) { s += '*'; } a.yang_type ? console.log(s + a.yang_type + ' ' + a.name) : console.log(s + a.name + ' ' + a.value); Array.isArray(a.children) && a.children.forEach(loop(level + 1)); } } var data = [{ "yang_type": "container", "name": "c1", "value": "", "children": [{ "yang_type": "", "name": "type", "value": "Uint32", "children": [] }, { "yang_type": "list", "name": "DNS", "value": "", "children": [{ "name": "type", "value": "String", "children": [], "yang_type": "" }, { "yang_type": "leaf", "name": "ip-address", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }, { "yang_type": "leaf", "name": "Domain", "value": "", "children": [{ "name": "type", "value": "string", "children": [], "yang_type": "" }] }] }] }]; data.forEach(loop(0)); 

暂无
暂无

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

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