以下のようなjsonコードのためにどのように $ each
を行うことができますか?
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"
}
]
}
]
}
私の試みはこれです(仕事ではありません):
$.ajax({
...
success: function (data) {
$.each(data.reunits['reun'], function (index, value) {
$('.li_show').append(''+value.name+' ');
});
}
)}
次の操作を実行できます。
$.ajax({
...
success: function (data) {
$.each(data.reunits, function (index, value) {
for(key in value){
$.each(value[key], function(i, v){
$('.li_show').append(''+v.name+' ');
});
}
});
)}
for
ステートメントを使用して、配列項目内の内部オブジェクトを反復処理することができます
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(''+g.name+' ');
});
});
});
編集
$(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(''+$var+' ');
});
});