0

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!!

Community
  • 1
  • 1
user1234
  • 3,000
  • 4
  • 50
  • 102
  • 1
    have you looked into the [concat method of Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)? `self.series.concat(self.series2);` ? – Patrick Evans Jun 05 '15 at 23:06
  • possible duplicate of [How to merge two arrays in Javascript and de-duplicate items](http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Lumi Lu Jun 08 '15 at 00:29

2 Answers2

3

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

var series = series1.concat(series2);
frontend_dev
  • 1,693
  • 14
  • 28
0

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

Lucas Ross
  • 1,049
  • 8
  • 17