简体   繁体   中英

Traverse JSON object in Javascript?

below is my code. please help me loop through it. i want to loop the complete JSON and do some validation, but i am not able to loop through it. i am doing it for the first time , it would be nice if any1 can help me.

Is there any way to filter the JSON object. for example i want to search auditor1 asgn value. filter can be dynamic like it can be auditor1 or auditor11. also i want to knw how can i convert the above json into array. which will make my search easy(in case there is no way to search by direct JSON search).

function fnMultiRowValidation(){
      var vStatus = 5,
          vJson = '{"tpaCo":[{"name":"Audit Company1",\
                             "aud":[{"name":"auditor1","asgn":"1","fnds":"1","lead":"1"},\
                                    {"name":"auditor2","asgn":"1","fnds":"0","lead":"1"},\
                                    {"name":"auditor3","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor5","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor6","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor7","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor8","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor9","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor10","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor11","asgn":"1","fnds":"1","lead":"0"}]},\
                            {"name":"Audit Company2",\
                             "aud":[{"name":"auditor3","asgn":"1","fnds":"1","lead":"1"},\
                                    {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"}\
                                   ]\
                            }\
                          ]}';
          var vObj =  JSON.parse(vJson);


      for (var i=0;i<vObj.tpaCo.length;i++){
        $.each(vObj.tpaCo[i], function(key, value) { 
              console.log(key +':'+ value);
              if(typeof(value)=='object'){
                 //console.log('Auditor length:'+vObj.tpaCo.value.length);
              }
        });  
      }
    }
 vObj.tpaCo.value.length 

won't work. You either had to use vObj.tpaCo[key].length or value.length . For a beginner, you shouldn't mix native for-loops with each iteration.

Using for - and for-in -loops:

for (var i=0; i<vObj.tpaCo.length; i++) { // iterate through outer array
    for (var key in vObj.tpaCo[i]) { // enumerate item keys
         console.log(key +':'+ vObj.tpaCo[i][key]); // logs "name" and "aud"
    }
    console.log('Auditor length:'+vObj.tpaCo[i].aud.length);
    for (var j=0; j<vObj.tpaCo[i].aud.length; j++) { // iterate "aud" array
        console.log(vObj.tpaCo[i].aud[j].name);
    }
}

Simplified by using variables:

var tpacos = vObj.tpaCo;
for (var i=0; i<tpacos.length; i++) {
    var comp = tpacos[i];
    for (var key in comp) {
         var value = comp[key];
         console.log(key +':'+ value);
    }
    var auds = comp.aud;
    console.log('Auditor length:'+auds.length);
    for (var j=0; j<auds.length; j++) {
        var aud = auds[j];
        console.log(aud.name);
    }
}

Now with the Array forEach method :

vObj.tpaCo.forEach(function(comp, i) {
    for (var key in comp) {
         var value = comp[key];
         console.log(key +':'+ value);
    }
    console.log('Auditor length:'+comp.aud.length);
    comp.aud.forEach(function(aud, j) {
        console.log(aud.name);
    });
});

And with jQuery's each :

$.each(vObj.tpaCo, function(i, comp) {
    $.each(comp, function(key, value) {
        console.log(key +':'+ value);
    });
    console.log('Auditor length:'+comp.aud.length);
    $.each(comp.aud, function(j, aud) {
        console.log(aud.name);
    });
});

your code should be like this

$.each(vObj.tpaCo, function(key, value) { 
          console.log(key +':'+ value);
          if(typeof(value)=='object'){
             //console.log('Auditor length:'+vObj.tpaCo.value.length);
          }
});

hope it helps.....

  function fnMultiRowValidation(){
  var vStatus = 5,
      vJson = '{"tpaCo":[{"name":"Audit Company1",\
                         "aud":[{"name":"auditor1","asgn":"1","fnds":"1","lead":"1"},\
                                {"name":"auditor2","asgn":"1","fnds":"0","lead":"1"},\
                                {"name":"auditor3","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor5","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor6","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor7","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor8","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor9","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor10","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor11","asgn":"1","fnds":"1","lead":"0"}]},\
                        {"name":"Audit Company2",\
                         "aud":[{"name":"auditor3","asgn":"1","fnds":"1","lead":"1"},\
                                {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"}\
                               ]\
                        }\
                      ]}';
      var vObj =  JSON.parse(vJson);


    $.each(vObj.tpaCo, function(key, value) { 
          console.log(value.name);
    });  
}

remove for loop. Use this script

To get aud elements, u need to have a $.each function inside the current loop

I'm pretty sure you can get rid of the for loop, and just use

$.each( vObk.tpaCo, function(key, value) {
  console.log(key + ':' + value);
  //... do stuff with value ...
})
$.each(vObj.tpaCo, function(key, value) { 
              console.log(value.name+" ")
              for(i=0; i<value.aud.length; i++)
              console.log(value.aud[i]);
        }); 

or if you don't know the names

       $.each(vObj, function(key, value){
        $.each(value, function(key, value){
            for(val in value)
                  if(typeof val == 'string')
                    console.log(value[val]
                  else 
                  for(i=0; i<value[val].length ; i++)
                    console.log(value[val][i]);

        });
    });

http://jsfiddle.net/GxER7/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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