Merge Arrays with JavaScript

By  on  

Merging arrays is a fairly common occurrence.  I remember when I worked a lot with PHP I would use array_merge() all the time.  I found myself merging arrays often when handling form submission.

JavaScript has a simple, native function for merging arrays (concat) but it produces a new array. Since JavaScript vars are passed by reference, concat may mess up a reference. If you want to merge a second array into an existing first array, you can use this trick:

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
Array.prototype.push.apply(array1, array2);

console.log(array1); // is: [1, 2, 3, 4, 5, 6]

Using an Array.prototype method, push in this case, allows you to merge the second array into the first.  The alternative is iterating through the second array and using push on the first array.  This shortcut is niiiiiiiiiice!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Telephone Link Protocol

    We've always been able to create links with protocols other than the usual HTTP, like mailto, skype, irc ,and more;  they're an excellent convenience to visitors.  With mobile phone browsers having become infinitely more usable, we can now extend that convenience to phone numbers: The tel...

  • By
    Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

Discussion

    • concat returns a new array, doesn’t merge into the first one.

    • Ilya

      Yes, it returns new array, but you didn’t say that in post

  1. Valtteri

    With ES6 spread operator:

    var array1 = [0, 1, 2];
    var array2 = [3, 4, 5];
    array1.push(...array2);
    

    Source: MDN

  2. Nikhil

    Why is

    Array.prototype.push.apply(array1, array2);

    different from

    Array.push.apply(array1, array2);

    , which returns

    [1,2,3]
  3. There’s a concat method for this purpose.

    • Again, concat creates a new array. My method in the post does not.

  4. Ryan Murphy

    I wish you posted this 2 days ago :) I went back and refactored.
    Thanks!

  5. Why not

    array2.push.apply(array1, array2);

    ?

    • Val

      I guess that’s just a matter of taste. I personally wouldn’t like to to reference the same variable twice in the same line.

    • Ilya

      how do you prepend a string to variable? :)
      I mean how do you write the following:

      str = 'foo' + str;
      
  6. This can be shortened just a little:

    array1.push.apply(array1, array2);
    

    Although as mentioned above the ES6 spread operator is great for concatenation.

  7. chris-l

    If you miss for some reason array_merge, then you can do:

    var array_merge = Function.prototype.apply.bind(Array.prototype.push);

    Then you can use like this:

    array_merge(array1, array2);

    (IMHO, using concat is better. Returning a new array instead of changing one of the arguments is more “functional”. But of course, I’m pretty sure there are cases where changing an existing array may be required)

  8. Berkana

    You could save yourself a bunch of typing by doing

    [].push.apply(array1, array2);
    

    since the empty array will look up the prototype chain to find the push method, then apply it.

    • IlyaVF

      Although the result is the same, but in your variate there is are two extra steps of creating a new array and looking through the prototype chain.

  9. Jay Doubleyou

    Thanks for sharing!

  10. Nick

    A new array via concat is preferable to a mutative merge!

  11. Ray

    I tend to discourage this type of thing as opposed to properly using concat. As it can be difficult, at first glance for other devs to figure out what the intent is.

  12. I think that a lot of people are missing the point. concat and [].push.apply(array1, array2) would be used in different situations.

    concat is used if you *want* a new array – immutability is an example.

    [].push.apply(array1, array2) – is used if you need to maintain a reference to the primary array. e.g. it’s a property of an object that you’re iterating through and is referenced elsewhere.

  13. Thanks for sharing this trick!

  14. Beware of apply. A too large array2 will result in a Uncaught RangeError: Maximum call stack size exceeded.

    E.g. this will crash in google chrome (with lodash):

    var arr = [1,2];
    arr.push.apply(arr, _.range(0, 500000));
    
  15. IlyaVF

    This is a nice trick. Just wanted to add some explanation why this is working.

    The push method expects comma separated arguments to be used as a new elements for the array to push to.

    The apply method expects two arguments: the first one to be used as a context (this) for the method, and the second one as an array of arguments to be applied to the method.

    So the trick is that when you use _apply_ the _array1_ is used a context for push method and array2 in the original example will be used as an array of arguments for the push method.

    And it does not matter if you use [].push or array1.push or array2.push or Array.prototype.push. The apply method will replace the context (this) for the method according to its first argument. Only Array.prototype.push is the fastest (there is no prototype chain lookup).

  16. Ctibor

    Thats really nice shortcut.. I love it :)

  17. Ajay
    array1 = array1.concat(array2);

    This serves the same output. So what is the difference between using this method and Array.prototype.push.apply(array1, array2)?

  18. Aaron

    “Since JavaScript vars are passed by reference, concat may mess up a reference.”

    What does this mean? Mutating a reference IS messing up the reference. This is a performance optimization that could lead to subtle bugs. In other words, you should probably stick to concat unless you have a good reason for mutation.

    • IlyaVF

      I think by this statement the author meant something like this:

      var a = [1,2,3];
      var b = a;
      a = a.concat([4,5]);
      a
      // -> [1,2,3,4,5];
      b
      // -> [1,2,3]
      

      Whereas using push method _b_ would point to the same array as _a_.

  19. Adam

    This was covered previously on DWB: http://davidwalsh.name/combining-js-arrays

  20. noproblemo

    concat useful to flattening array , example :

      var arr=[[0],[1],[2],[3],[4],[5]];
      var rslt=[].concat.apply([],arr);
      console.log(rslt)//[0,1,2,3,4,5]
    

    is there a better way ???

  21. push and pop add/remove elements from the end of the array unshift / shift — add/remove elements from the beginning of the array splice — add/remove elements from the specified location of the array.

  22. joe hoeller

    How do you make an array of arrays?

    $scope.result = [ ];
    
    //do stuff here to push the arrays into the above array so the result look like this:
    
    $scope.result = [  [1,2], [3,4], [5,6]  ];
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!