4

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 ?

2
  • Please do not edit your question to include a new problem. Instead, create a new question. Commented Jun 22, 2016 at 12:04
  • If i do that. Ppl mark it as duplicate :) anyways let me create a new one Commented Jun 22, 2016 at 12:11

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.