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

asked Jun 5, 2015 at 23:04
2

2 Answers 2

3

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

var series = series1.concat(series2);
answered Jun 5, 2015 at 23:08
0

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

answered Jun 5, 2015 at 23:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.