52

As described here, a quick way to append array b to array a in javascript is a.push.apply(a, b).

You'll note that the object a is used twice. Really we just want the push function, and b.push.apply(a, b) accomplishes exactly the same thing -- the first argument of apply supplies the this for the applied function.

I thought it might make more sense to directly use the methods of the Array object: Array.push.apply(a, b). But this doesn't work!

I'm curious why not, and if there's a better way to accomplish my goal. (Applying the push function without needing to invoke a specific array twice.)

1
  • +1 for discovering Firefox's Array.push, even if by accident. :-) Commented Mar 16, 2013 at 1:35

4 Answers 4

72

It's Array.prototype.push, not Array.push

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

4 Comments

Ah, thanks. For some reason Array.push is defined in Firefox, but not in Chrome. (At least in the respective javascript consoles.) Using the prototype method works just fine. Figures it was something relatively obvious. :)
@starwed—note that in Firefox, Array.push !== Array.prototype.push, wonder what it does?
For future reference, in Firefox, Array.push(a, b, c) is equivalent to Array.prototype.push.apply(a, b, c). It's just for convenience.
note you can also use an instance of an array to append another array like so: (in node REPL) var a = [1,2,3,4];var b = [5,6,7,8]; a.push.apply(a,b) returns '[ 1, 2, 3, 4, 5, 6, 7, 8 ]` in response to '>a'
11

The current version of JS allows you to unpack an array into the arguments.

var a = [1, 2, 3, 4, 5,];
var b = [6, 7, 8, 9];

a.push(...b); //[1, 2, 3, 4, 5, 6, 7, 8, 9];

1 Comment

Though I just did a very quick and dirty performance test and it looks like [].push.apply(a,b); is slightly faster in this case in my browser and concat is slightly slower.
10

You can also use [].push.apply(a, b) for shorter notation.

3 Comments

For shorter notation you can use a.push(b)
@EugeneMala No, that would result in the whole array "b" being pushed into "a" as a new element: var a = [1,2], b = [3,4]; a.push(b); // [1, 2, Array[2]]
Note that [].push.apply(a, b) creates an unneeded new Array instance [], whereas a.push.apply(a, b) uses the existing one a
4

What is wrong with Array.prototype.concat?

var a = [1, 2, 3, 4, 5];
var b = [6, 7, 8, 9];

a = a.concat(b); // [1, 2, 3, 4, 5, 6, 7, 8, 9];

4 Comments

That's not really relevant here (my question wasn't about how to join two arrays) -- but the distinction is made clear in the question I linked to. concat returns a new function, rather than appending b to a.
Fair enough :) And it actually turns out Array.prototype.push.apply(a, b), is much faster: jsperf.com/arrayconcatvsarraypushapply
@phenomnomnominal, that's not necessarily true in all cases. The jsperf you link to extends the Array length in each loop, which of course is going to make the copying concat slower at each iteration, but if the Array length stays fixed through each iteration then concat can perform better, as seen here: jsperf.com/array-prototype-push-apply-vs-concat/5 (looking through all the other revisions reveals many neat approaches to this simple problem)
FWIW: a.push(4, 5, 6) adds the elements to the existing array a, whereas a = a.concat([4, 5, 6]) creates a new array instance and overwrites the variable a. Also consider var a = [1,2,3], a2 = a;, then a2 would stay unchanged with concat, but changes with push.

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.