2017-03-07 30 views
0

是否可以合并两组数据data.postsdata.comments以便它们都在我的循环中响应?将两组返回的json数据合并为循环

success: function(data) { 
    datas = data.posts; 
    datas = data.comments; 
    $.each(datas, function(i, response) { 
    )} 
    } 

回答

1

当然:

success: function(data) { 
var datas = {}; 
datas = data.posts; 
datas = data.comments; 
for (var postIndex in data.posts) { 
    datas[postIndex] = data.posts[postIndex]; 
} 
for (var commentIndex in data.comments) { 
    datas[commentIndex] = data.comments[commentIndex]; 
} 
$.each(datas, function(i, response) { 
)} 
} 

但你必须知道,如果postscomments有一些类似的按键,后者将覆盖前面的合并结果。如果您不满意,请添加更多信息。

+0

谢谢你。 – Gateway

+0

不客气。 –