2

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);
              }
        });  
      }
    }
1
  • Try not to use $.each but understand for- and for-in-loops first. Commented Aug 27, 2013 at 9:43

5 Answers 5

1
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);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
  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

Comments

0

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 ...
})

Comments

0

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.....

4 Comments

No, really not. You're iterating a boolean.
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).
check this link for searching Json Search
and for converting json to array check this Link
0
$.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/

3 Comments

How do i get the property name of JSON in JS. i wanted to make this code dynamic. is it possible ? instead of using aud or name. i want it to be dynamic. is it possible ?
typeof val is always string.
actually name field is string, the other is object.

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.