Get json output in $ .each

My json details:

{ "guide":0, "reunits":[ { "residence":[ { "name_re":"THE PORT", "id":"88aa355ca54853640929c25c33613528" } ] }, { "residence":[ { "name_re":"KECIK", "id":"2843543fa45857d92df3de222938e84a" } ] }, { "residence":[ { "name_re":"GREEN ANKA", "id":"fe585cc4b4dfff1325373728929e8af9" } ] } ] } 

How can I make an alert the value of name_re or id in json data is higher than each in jquery?

My attempt:

 $.each(data.reunits, function (index, value) { alert(value.residence.name_re); // this don't output. }) 
+4
source share
5 answers

residence is an array ( [] ) that has only one element, an object with the name_re attribute.

 alert(value.residence[0].name_re); 

jsFiddle Demo

+3
source

This is because they are inside arrays. You need to get index 0 for each of them:

 $.each(data.reunits, function(index, value) { alert(value.residence [0] .name_re); }); 
+2
source

Location is an array, so you need to do the following js fiddle

  $(document).ready(function() { var data = { "guide": 0, "reunits": [ { "residence": [ { "name_re": "THE PORT", "id": "88aa355ca54853640929c25c33613528"} ]}, { "residence": [ { "name_re": "KECIK", "id": "2843543fa45857d92df3de222938e84a"} ]}, { "residence": [ { "name_re": "GREEN ANKA", "id": "fe585cc4b4dfff1325373728929e8af9"} ]} ] }; $.each(data.reunits, function() { $.each(this.residence, function() { alert(this.name_re); alert(this.id); }); }); }); 
+1
source
  var data = JSON;
 var reunits = data.reunits;
 for (var i = 0, len = reunits.length; i <len; i ++) {
     alert (reunits [i] .residence [0] .name_re);
 }
0
source

value.residence is an array. This should be value.residence[0].name_re .

 $.each(data.reunits, function (index, value) { alert(value.residence[0].name_re); }) 
0
source

Source: https://habr.com/ru/post/1387804/


All Articles