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.)

Community
  • 1
  • 1
starwed
  • 2,536
  • 2
  • 25
  • 39

4 Answers4

72

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

Satpal
  • 132,252
  • 13
  • 159
  • 168
Ven
  • 19,015
  • 2
  • 41
  • 61
  • 2
    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 Mar 16 '13 at 00:42
  • @starwed—note that in Firefox, `Array.push !== Array.prototype.push`, wonder what it does? – RobG Mar 16 '13 at 00:58
  • 7
    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. – Casey Chu May 12 '13 at 10:35
  • 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' – unsynchronized Jun 08 '17 at 03:47
10

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

erdem
  • 195
  • 2
  • 14
  • For shorter notation you can use a.push(b) – Eugene Mala Sep 22 '16 at 00:13
  • 18
    @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]]` – Đinh Carabus Dec 23 '16 at 20:34
  • Note that `[].push.apply(a, b)` creates an unneeded new Array instance `[]`, whereas `a.push.apply(a, b)` uses the existing one `a` – kca Aug 11 '21 at 05:30
10

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];
David Griffin
  • 171
  • 1
  • 5
  • 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. – David Griffin Feb 27 '19 at 16:27
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];
phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
  • 4
    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. – starwed Mar 16 '13 at 00:43
  • 11
    Fair enough :) And it actually turns out `Array.prototype.push.apply(a, b)`, is *much* faster: http://jsperf.com/arrayconcatvsarraypushapply – phenomnomnominal Mar 16 '13 at 00:47
  • 3
    @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: http://jsperf.com/array-prototype-push-apply-vs-concat/5 (looking through all the other revisions reveals many neat approaches to this simple problem) – CJ Gaconnet Jun 08 '14 at 22:28
  • 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`. – kca Aug 11 '21 at 05:56