I have a javascript array dataArray which I want to push into a new array newArray. Except I don't want newArray[0] to be dataArray. I want to push in all the values into the new array:

var newArray = [];

newArray.pushValues(dataArray1);
newArray.pushValues(dataArray2);
// ...

or even better:

var newArray = new Array (
   dataArray1.values(),
   dataArray2.values(),
   // ... where values() (or something equivalent) would push the individual values into the array, rather than the array itself
);

So now the new array contains all the values of the individual data arrays. Is there some shorthand like pushValues available so I don't have to iterate over each individual dataArray, adding the values 1 by 1?

share|improve this question

3 Answers

up vote 19 down vote accepted

Use the concat function. You can see an example of it in use here:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/concat

share|improve this answer
3  
2  
Slow though: jsperf.com/concat-vs-push-apply/10 – w00t Aug 20 '12 at 19:42
This is a bad answer. Two links that are identical, and no sample code. Not good – musefan Feb 22 at 12:38
The answer below is much more detailed, I agree- wish I could assign that as the correct answer. – WiseGuyEh Feb 22 at 15:38

You can use the push() method of the array to which you wish to append values. push() can take multiple parameters so you can use its apply() method to pass the array of values to be pushed as a list of function parameters. This has the advantage over using concat() of adding elements to the array in place rather than creating a new array:

var newArray = [];
newArray.push.apply(newArray, dataArray1);
newArray.push.apply(newArray, dataArray2);

You might want to generalize this into a function:

function pushArray(arr, arr2) {
    arr.push.apply(arr, arr2);
}

... or add it to Array's prototype:

Array.prototype.pushArray = function(arr) {
    this.push.apply(this, arr);
};

var newArray = [];
newArray.pushArray(dataArray1);
newArray.pushArray(dataArray2);
share|improve this answer
Why not just generalize it into a prototype function: Array.prototype.pushArray(arr) { this.push.apply(this, arr); } ... and then: newArray.pushArray(dataArray1); – Šime Vidas Nov 11 '10 at 15:44
@Šime: Yes, good idea. I'm not in the habit of extending the prototypes of built-in objects but it's an option. I'll add it. – Tim Down Nov 11 '10 at 15:46
I mean Array.prototype.pushArray = function(arr) { ... of course. My above code is incorrect. – Šime Vidas Nov 11 '10 at 15:49
@Šime: Yeah, I realized :) – Tim Down Nov 11 '10 at 15:51
var a=new Array('a','b','c');
var b=new Array('d','e','f');
var d=new Array('x','y','z');
var c=a.concat(b,d)

Does that solve your problem ?

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.