3

I have three arrays:

var arrayOne=[{obj1}, {obj2}, {ob3}];
var arrayTwo=[{obj4}, {obj5}, {obj6}];
var arrayThree=[{obj7}, {obj8}, {obj9}];

And I need to Know how to fill a new array with values from those arrays, like this:

var arrayFINAL=[{obj1}, {obj2}, {ob3}, {obj7}, {obj8}, {obj9}, {obj4}, {obj5}, {obj6}];

I thought it was something like this:

var arrayFINAL = new Array(arrayOne, arrayTwo, arrayThree);

But it seems to create an array an array's of arrays not an objects array. Anyone knows how to do this? thnks!

1
  • ... why are you switching arrayTwo and arrayThree in your example? Commented May 11, 2012 at 0:40

5 Answers 5

3
var combinedArray = arrayOne.concat(arrayTwo, arrayThree);

MDN

Syntax
array.concat(value1, value2, ..., valueN)

Live DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

You're looking for Array concatenation

arrayFinal = arrayOne.concat(arrayTwo, arrayThree);

See here for documentation

Comments

0
var arrayFinal = arrayOne.concat(arrayThree,arrayTwo);

1 Comment

All of the answers are the same!
0

Your code...

var arrayFINAL = new Array(arrayOne, arrayTwo, arrayThree);

Will make an array arrayFINAL with three members, each of which is another array, like a multidimensional array.

If you want to create a new array with all three, use the concat() operator...

var arrayFinal = arrayOne.concat(arrayTwo, arrayThree);

jsFiddle.

If you want to push them all onto the original array, use...

arrayOne.push.apply(arrayOne, arrayTwo.concat(arrayThree));

jsFiddle.

Comments

-1

You may be interested in array_merge from PHJS - the behaviour you're looking for is identical to what PHP's array_merge does, and the PHPJS version does the same in JavaScript.

Comments

Your Answer

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.