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

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

    Use Array.extend instead of Array.push for > 150,000 records.

    if (!Array.prototype.extend) {
      Array.prototype.extend = function(arr) {
        if (!Array.isArray(arr)) {
          return this;
        }
    
        for (let record of arr) {
          this.push(record);
        }
    
        return this;
      };
    }
    

提交回复
热议问题