0

Is it possible to merge two sets of data data.posts and data.comments so they all come under response in my loop?

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

1 Answers1

1

Of course:

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) {
)}
}

But you must know that if posts and comments have some similar keys, the latter will overwrite the former in the merge result. If you are not ok with that, then please add more information.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175