0

I have two objects that look like this:

var a = [{ id: 0, name: "Zero" }]; var b = [{ id: 1, name: "one", firstName: "First" }], [{ id: 2, name: "two", firstName: "Second" }], [{ id: 3, name: "three", firstName: "Third" }]; 

I want to concat the two objects in Javascript to look something like this:

var c = [{ id: 0, name: "Zero" }], [{ id: 1, name: "one", firstName: "First" }], [{ id: 2, name: "two", firstName: "Second" }], [{ id: 3, name: "three", firstName: "Third" }]; 

Is there an easy way to do this?

2
  • 3
    Can you reclarify the structures you have/wish to have - what you've posted isn't valid syntax Commented Mar 24, 2015 at 9:37
  • what exactly c and b are? you do realize c equals only to the first array , all the others array has no meaning. assuming this doesn't throw exception at the first place Commented Mar 24, 2015 at 9:37

1 Answer 1

2

Although what you've posted doesn't look valid, your objects are in arrays so you can use .concat

var a = [{id: 0, name: "Zero"}]; var b = [ [{id: 1, name: "one", firstName:"First"}], [{id: 2, name: "two", firstName:"Second"}], [{id: 3, name: "three", firstName: "Third"}] ]; var c = a.concat(b); 

if there is no reason why your objects are in individual arrays, you'd want b to be:

var b = [ {id: 1, name: "one", firstName:"First"}, {id: 2, name: "two", firstName:"Second"}, {id: 3, name: "three", firstName: "Third"} ]; 
Sign up to request clarification or add additional context in comments.

Comments