user1234
user1234

Reputation: 3159

How to merge array of individual object into one single array-Js/Jquery

I'm trying to merge 2 objects into an array. I have a structure like this:

enter image description here

The object could be an empty or may have data. I'm trying to combine the object from he self.series2 into self.series, so as to look like:

enter image description here

where: the second object is the object from the self.series2. Is it possible?

Js:

var series = [{
  "Name": "abd",
  "Surname": "Agrawal",
  "id" : 7349879837,
  "address" : "7923street"
 }];
 var series2 = [{
  "Name": "abd23",
  "Surname": "desia",
  "id" : 7349879837,
  "address" : "7923street"

  }];
merge: function(){
   var self = this;
   var self.series = {};
   //here im not sure how to do a `forEach` for 2 objects
   $.each(series, function (i, o) {
       $.extend(self.series, o)
   });
   $.each(series2, function (i, o) {
       $.extend(self.series, o)   // in this case it overwrites the first object with the second one.
   });
});

How can i loop in through both the objects so that at the end in self.series, i have both objects from series and series2?

enter image description here

Any ideasss??? Thanks!!

Upvotes: 0

Views: 732

Answers (2)

Lucas Ross
Lucas Ross

Reputation: 1089

self.series = [self.series[0], self.series2[0]]

Upvotes: 0

frontend_dev
frontend_dev

Reputation: 1719

Seems you have two arrays, and want a merged one. So maybe you try with Array.concat?

var series = series1.concat(series2);

Upvotes: 3

Related Questions