javascript - Looping through a json array of objects -


i have been trying follow example per stackoverflow question:

javascript or jquery: looping multidimensional object

i posting question last resort have been on problem hours!

i have ajax function returns json array.

ajax:

  $.ajax({ url: 'functions.php',            data: {action: 'getnfcmapping', site: site_id},            type: 'post',            datatype: 'json',            success: function(data) {             //loop json                 (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 trying load form in dom input fields (pre-populated) in pairs of nfc_id , description, therefore each iteration of loop should output 2 values.

the problem @ moment can't access values in each iteration, e.g. can see trying log nfc_id of each iteration appears object in console.

in example ( javascript or jquery: looping multidimensional object ), can see difference in format of json, in have 2 closing brackets on each iteration of array, issue is?

please driving me crazy..

you want 2 nested .each() loops:

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

fiddle


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -