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

4 Answers 4

2

You want two nested .each() loops:

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

Fiddle

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

2 Comments

Excellent, worked. Thanks @scrowler. Will accept once time limit passes.
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
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);
    }
}

Comments

-1

Perhaps try the .each jQuery method? Link

So for your success:

success: function(data){
    $(data).each(function(index, element){
      //log here
    }
}

Comments

-1
for(var i=0, i<data.length;i++)
$.each(data[i], function(key, val) {
    console.log(val.nfc_id);
    console.log(val.description);
});

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.