How to extend an existing JavaScript array with another array, without creating a new array

后端 未结 16 1885
深忆病人
深忆病人 2020-11-22 07:38

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

I want to achieve th

16条回答
  •  不思量自难忘°
    2020-11-22 07:47

    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.

提交回复
热议问题