简体   繁体   中英

$.each for value json, how is it?

How can done $.each for following json code as this(for name):

1: 11 11
2: 666666666 99999 777777 1221
3: 55555 00000000 222222222 333333333

{
    "reunits": [
        {
            "reun": [
                {
                    "name": "11",
                    "price": "77192276",
                    "extra": "11",
                    "hotel_id": "77192276"
                },
                {
                    "name": "11",
                    "price": "77192276",
                    "extra": "11",
                    "hotel_id": "77192276"
                }
            ]
        },
        {
            "reun": [
                {
                    "name": "666666666",
                    "price": "15190364",
                    "extra": "11",
                    "hotel_id": "15190364"
                },
                {
                    "name": "99999",
                    "price": "15190364",
                    "extra": "11",
                    "hotel_id": "15190364"
                },
                {
                    "name": "777777",
                    "price": "15190364",
                    "extra": "11",
                    "hotel_id": "15190364"
                },
                {
                    "name": "1221",
                    "price": "15190364",
                    "extra": "11",
                    "hotel_id": "15190364"
                }
            ]
        },
        {
            "reun": [
                {
                    "name": "55555",
                    "price": "11",
                    "extra": "33",
                    "hotel_id": "15183965"
                },
                {
                    "name": "00000000",
                    "price": "11",
                    "extra": "33",
                    "hotel_id": "15183965"
                },
                {
                    "name": "222222222",
                    "price": "11",
                    "extra": "33",
                    "hotel_id": "15183965"
                },
                {
                    "name": "333333333",
                    "price": "11",
                    "extra": "33",
                    "hotel_id": "15183965"
                }
            ]
        }
    ]
}

My try is this(not work):

$.ajax({
...
success: function (data) {
    $.each(data.reunits['reun'], function (index, value) {
                    $('.li_show').append('<li>'+value.name+'</li>');
                });
            }
)}
$.each(data.reunits, function (index, value) {
    var parts = [];
    $.each(value.reun, function(k,v){
        parts.push(v.name);
    });              
    $('.li_show').append('<li><b>' + (index + 1) + ':</b> ' +parts.join(" ")+'</li>');   
});

You can do the following:

$.ajax({
...
success: function (data) {
    $.each(data.reunits, function (index, value) {
       for(key in value){
          $.each(value[key], function(i, v){
             $('.li_show').append('<li>'+v.name+'</li>');
          });
       }             
    });
)}

You can iterate inner objects inside your array item by using for statement

reunits in an array containing objects that have a reun property which is array of objects. You first need to loop through the reunits array, and then loop through each reun array.

$.each(data.reunits, function(){
  var str = '';
  $.each(this.reun, function(){
    str += this.name+' ';
  });
  $('.li_show').append($.trim(str));
});
$(data.reunits).each(function(i,j){
$.each(j,function(x,y){
$.each(y,function(f,g){
console.log(g.name);
 $('.li_show').append('<li>'+g.name+'</li>');

  });
 });
});

edit

$(data.reunits).each(function(i,j){
$.each(j,function(x,y){
$var=" ";
$.each(y,function(f,g){
$var+=" "+g.name;
//console.log(g.name);
  });
   $('.li_show').append('<li>'+$var+'</li>');
 });
});

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM