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

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

    Combining the answers...

    Array.prototype.extend = function(array) {
        if (array.length < 150000) {
            this.push.apply(this, array)
        } else {
            for (var i = 0, len = array.length; i < len; ++i) {
                this.push(array[i]);
            };
        }  
    }
    

提交回复
热议问题