Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

There doesn't seem to be a way to append an array to an existing JavaScript Array, i.e. to emulate Python's extend method.

What I want to achieve is the following:

>>> a = [1, 2]
[1, 2]
>>> b = [3, 4, 5]
[3, 4, 5]
>>> SOMETHING HERE
>>> a
[1, 2, 3, 4, 5]

I know there's a a.concat(b) method, but it creates a new array instead of simply extending the first one. I'd like an algorithm that works efficiently when a is significantly larger than b (i.e. one that does not copy a).

share|improve this question

4 Answers

up vote 157 down vote accepted

The best I came up with so far is:

>>> a.push.apply(a, b)

but it feels a little like a hack. Any other suggestions?

share|improve this answer
9  
This is exactly how you do it. Why does it feel like a hack? – kangax Sep 3 '09 at 15:41
3  
I think this is your best bet. Anything else is going to involve iteration or another exertion of apply() – Peter Bailey Sep 3 '09 at 15:43
7  
This is clever, but a bit hard to understand at a glance. – Evgeny Jun 14 '12 at 1:36
2  
@Evgeny You're right, that's why it feels like a hack :) – DzinX Jun 14 '12 at 14:12

It is possible to do it using splice():

b.unshift(b.length)
b.unshift(a.length)
Array.prototype.splice.apply(a,b) 
b.shift() // restore b
b.shift() //

But despite being uglier it is not faster than push.apply, at least not in Firefox 3.0. Posted for completeness sake.

share|improve this answer
4  
I have found the same thing, splice doesn't provide performance enhancements to pushing each item until about 10,000 element arrays jsperf.com/splice-vs-push – Drew May 22 '11 at 14:59
+1 Thanks for adding this and the performance comparison, saved me the effort of testing this method. – jjrv Dec 5 '12 at 11:38

i like the "a.push.apply(a, b)" method described above, and if you want you can always create a library function like this:

Array.prototype.append = function(array)
{
    this.push.apply(this, array)
}

and use it like this

a = [1,2]
b = [3,4]

a.append(b)
share|improve this answer

I've been using DzinX's answer for a while, but unfortunately it fails for when the array to extend by is large (tests in Chrome show that for me large is > 150000 entries, approx, in Firefox large for me is > 500000 entries).

If the array to extend by is too large the call stack size is exceeded when 'apply' is called in DzinX's solution.

The best way forward in these cases is to use a for loop. Unfortunately in Chrome the for loop method tends to be slower in general cases by about 6%. On the other hand in Firefox using a for loop is faster than DzinX's method by about 25%. (YMMV!)

Check out this js-perf for a comparison so that you can test this for yourself: http://jsperf.com/array-extending-push-vs-concat

So the "works for all array sizes" solution is:

Array.prototype.extend = function(array) {
    for (var i = 0, len = array.length; i < len; ++i) {
        this.push(array[i]);
    };    
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.