Using protoype.js I discovered a quick way to merge to arrays in javascript:
function array_merge(one, two) { one.push(two); return one.flatten(); }
This definitely beats the alternative of iterating through the second array by hand and adding it to the first. Any comments or suggestions are welcome.




You can do this even quicker!
one = one.concat(two);
Job done!
By Rumble on Aug 7, 2007
concat is KING
By Scott Meves on Jun 5, 2008
Another example for merge:
By Mrsoul on Aug 11, 2008
Following is my version of independent array_merge (compact):
By Subhasis Deb on Nov 25, 2008
@Subhasis Deb: Thanks! Sorry our comment code formatting is so messed up.
By Scott Meves on Nov 26, 2008
I have a prototype that handles more complex merging of arrays as well as inserting arrays into other arrays all without needing a for / while loops. Read more about it here: http://gregorytomlinson.com/encoded/2008/12/25/javascript-array-merge/
By gregory on Dec 26, 2008
I like how Subhasis Deb’s function differentiates between Arrays and Objects, since Objects functionally simulate PHP associative arrays, and more often than not are what I use for handling data. However, there are a few things I would recommend changing. First of all, the for…in loop pattern is incredibly inefficient with arrays, and I also never recommend using it for readability purposes. I switch between JavaScript and PHP all the time, and in PHP, we have the notation foreach ($var as $arr) to loop through the array, where $var is the value of the current iteration. However, in JavaScript, the for…in notation simply initializes an index. This isn’t a big deal if you work with JavaScript all the time, but I’ve been tripped up by more than one loop where I was expecting the val in for (var val in myArr) to be equal to one of myArr’s values, rather than an interator. Secondly, rather than using a loop, you can use concat, which is a native method and nearly infinitely faster than using a JavaScript loop.
There is one major bug in this function if you’re scripting in a multi-frame DOM environment. Objects in different windows do not share their constructor with the objects in the window that spawned them, so the evaluation [].constructor == Array is actually false in child windows. Since this check is false, the next check asks if the arguments are an instance of Object, which is always true since everything is an Object in JavaScript.(i.e. [] instanceof Object evaluates true). The solution to this is to call the constructor in the context of the global Object object using the apply method:
Object.prototype.toString.apply([]); // == “[object Array]”
This also leaves out array-like values, which are very important in DOM scripting, since document.getElementsByTagName doesn’t return an Array, it returns a NodeList, which is not an instanceof Array. Because of these array-like objects, Arrays are part of a broader group of objects called Collections. For this, I picked up this handy check from the YUI source code to check if an element is a collection (Arrays included):
I might rewrite this function to be sensitive to array-like objects and be a little more compatible with multi-frame environments.
By Andrew Noyes on Apr 26, 2009
Here’s my take on array_merge for JavaScript:
http://gist.github.com/102384
For better or for worse, it allows collections to be merged into arrays. It checks the type of the objects strongly to make sure they’re compatible. It works fine in Firefox and Safari that I know of.
By Andrew Noyes on Apr 27, 2009
I didn’t test this solution for performances, but if you are looking for a simpler way to do it :
function array_merge(one, two) {
one.push.apply(one, two);
}
array_merge(one.clone(), two) avoids the side-effect
By MC on Sep 23, 2010