0

Possible Duplicate:
Combine 2 JSON objects in JQUERY

I'm creating a facebook app with node on the backend to get the album list and show the cover photo for each. It needs to be done with two calls, one to get a list of albums, another to get the cover photo with the id of a particular album

my code looks something like this

 $('#main').on('click', '#albums', function() {
      $.get('/albums', function(data) {
           for (i = 0; i < data.data.length; i++) {
                $.get('/cover-photo?id=' + data.data[i].cover_photo, function(dataz) {
                     data+=dataz;
                });
            }
            console.log(data);
       });
  });

the reason I want it all in one object is because I'm using javascript templating and it'd be better to just have one object that has everything

Community
  • 1
  • 1
archytect
  • 3,615
  • 5
  • 22
  • 30

3 Answers3

3

If you want to combine this object for using tepmplate enging I will suggest you just add a another key with the same object so you can access all information of album and also with that each album cover photo object.

Like:

$('#main').on('click', '#albums', function() {
 $.get('/albums', function(data) {
   for(i = 0; i < data.data.length; i++) {
    $.get('/cover-photo?id=' + data.data[i].cover_photo, function(dataz) {
      data.data[i].coverPhotoObj = dataz;
      //this new key with perticular album object will hold that cover photo object details
    });
  }
 console.log(data);
});});
Dhiraj
  • 1,119
  • 1
  • 13
  • 23
2

use

$.extend({},firstobject,secoundobject)
Hussein Nazzal
  • 2,557
  • 18
  • 35
2

Use jquery.extend :

jQuery.extend({}, json1, json2);
Iswanto San
  • 18,263
  • 13
  • 58
  • 79