javascript - looping through JSON object with multiple childrens using java script -


i trying loop through json object

    [       {         "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 trying logic 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 children. doing wrong ?

you try loop on array. attempt not work way.

you use callback iterating , take recursive call childrens.

function loop(a) {      console.log(a.name);                                   // process data      array.isarray(a.children) && a.children.foreach(loop); // check , 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 indented output.

function loop(level) {      return function (a) {          var = 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));


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -