2

I have been trying to follow an example as per another stackoverflow question:

javascript or jquery: Looping a multidimensional object

I am posting this question as a last resort as I have been on the problem for hours!

I have an ajax function that returns a json array.

ajax:

  $.ajax({ url: 'functions.php',
           data: {action: 'getNFCMapping', site: site_id},
           type: 'post',
           dataType: 'json',
           success: function(data) {
            //loop the json to get the
                for (var key in data) {
                      console.log(data[key].nfc_id);
                  }
            } //end success
  });//end ajax

json array:

[{"24":{"nfc_id":"1","description":"sdfgdsg"}},{"25":{"nfc_id":"2","description":"dfgdfgdfg"}},{"26":{"nfc_id":"3","description":"fdgdffg"}},{"27":{"nfc_id":"4","description":"dfgdfgdfg"}}]

What I am trying to do eventually is load a form in the DOM with input fields (pre-populated) in pairs of nfc_id and description, therefore each iteration of the loop should output the two values.

The problem at the moment is that I can't actually access the values in each iteration, e.g. you can see I am trying to log the nfc_id of each iteration but it just appears as object in the console.

In the example ( javascript or jquery: Looping a multidimensional object ), I can see the difference in format of my json, in that I have two closing brackets on each iteration of my array, is this what the issue is?

Please help this is driving me crazy..

Community
  • 1
  • 1
anewvision
  • 105
  • 2
  • 15

4 Answers4

2

You want two nested .each() loops:

$.each(data, function(key, value) {
    $.each(value, function(k, v) {
       console.log(v.nfc_id); 
    });
});

Fiddle

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Excellent, worked. Thanks @scrowler. Will accept once time limit passes. – anewvision Mar 10 '14 at 20:37
  • 1
    Just wanted to throw some caution in here because `$.each()` exhibits an exponential difference in processing time as the amount of array/object elements increases. A million iterations of empty array items takes 1500% longer when using `$.each()`. Of course this is going to vary from one CPU to another so my old AMD dual-core is probably below average of modern CPUs. **[My Test](http://jsfiddle.net/3G4vD/)** – MonkeyZeus Mar 10 '14 at 21:04
1

You data is an array, so you should use for (var i = 0; i < data.length; i++) loop. Each element is an object so you use var key in obj notation:

for (var i = 0; i < data.length; i++) {
    for (var key in data[i]) {
        console.log(data[i][key].nfc_id);
    }
}
dfsq
  • 191,768
  • 25
  • 236
  • 258
-1
for(var i=0, i<data.length;i++)
$.each(data[i], function(key, val) {
    console.log(val.nfc_id);
    console.log(val.description);
});
Peter Bratton
  • 6,302
  • 6
  • 39
  • 61
Shovan
  • 185
  • 7
-1

Perhaps try the .each jQuery method? Link

So for your success:

success: function(data){
    $(data).each(function(index, element){
      //log here
    }
}
biddano
  • 491
  • 5
  • 16