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

后端 未结 16 1874
深忆病人
深忆病人 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:48

    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)
    

提交回复
热议问题