Search code examples
javascriptjsonnestednested-loops

looping through JSON object with multiple childrens using java script


I am trying to loop through a Json object which would look like this

    [
      {
        "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": ""
                  }
                ]
              }
            ]
          }
        ]
      }
    ]

I am trying this logic but it doesnt loop through first childs child.

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];   
}

The above code doesn't loop through all the children. what i am doing wrong ?


Solution

  • You try to loop over the array. Your attempt does not work this way.

    You could use a callback for iterating and a take it for the recursive call for childrens.

    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);

    Edit with indented output.

    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));