Append JavaScript Array to another Array


Problem

Let's assume you've got an array, but instead of the usual case where you just append a single element to it, you want to append another array.



Solution

Let's assume that we've got two Arrays:

var a = [1, 2];
var b = [3, 4, 5];

The method push supports multiple arguments, we just have to use .apply to convert the Array into arguments and our original Array a contains 5 elements:

Array.prototype.push.apply(a,b)

Comments




Please sign-in to post a comment