Nodejs Array Splice splice(start, deleteCount)

Here you can find the source of splice(start, deleteCount)

Method Source Code

Array.prototype.splice = function (start, deleteCount) {
   var max = Math.max,
      min = Math.min,/*from   ww w .  j av a  2  s. c om*/
      delta,
      element,
      insertCount = max(arguments.length - 2, 0),
      k = 0,
      len = this.length,
      new_len,
      result = [],
      shift_count;
   start = start || 0;
   if (start < 0) {
      start += len;
   }
   start = max(min(start, len), 0);
   deleteCount = max(min(typeof deleteCount === 'number' ?
      deleteCount : len, len - start), 0);
   delta = insertCount - deleteCount;
   new_len = len + delta;
   while (k < deleteCount) {
      element = this[start + k];
      if (element !== undefined) {
         result[k] = element;
      }
      k += 1;
   }
   shift_count = len - start - deleteCount;
   if (delta < 0) {
      k = start + insertCount;
      while (shift_count) {
         this[k] = this[k - delta];
         k += 1;
         shift_count -= 1;
      }
      this.length = new_len;
   } else if (delta > 0) {
      k = 1;
      while (shift_count) {
         this[new_len - k] = this[len - k];
         k += 1;
         shift_count -= 1;
      }
   }
   for (k = 0; k < insertCount; k += 1) {
      this[start + k] = arguments[k + 2];
   }
   return result;
};

Related

  1. splice(a, b)
    Array.prototype.splice = function(a, b)
      var tmp = [];
      for (var i = a + b; i < this.length; i++)
        tmp[tmp.length] = this[i];
      var rem = [];
      for (i = a; i < a + b; i++)
    ...
    
  2. splice(from, count)
    'use strict';
    Array.prototype.splice = function (from, count) {
      function getArrayKeys(a) {
        var keys = [];
        for (var key in a) {
          keys.push(key);
        return keys;
      var keys = getArrayKeys(this);
      var poppedValues = [];
      for(var keyPosition in keys) {
        if (keyPosition < from) {
          continue;
        poppedValues.push(this.pop());
      var removedValues = [];
      for (var i = count ; i > 0 ; i-- ) {
        removedValues.unshift(poppedValues.shift());
      var args = Array.prototype.slice.call(arguments);
      args.shift();
      args.shift();
      for (var n in args) {
        this.push(args[n]);
      for (var x in poppedValues) {
        this.unshift(poppedValues[x]);
      return removedValues;
    };
    module.exports = Array.prototype.splice;
    
  3. splice(start, length)
    Array.prototype.splice = function(start, length){
        var len = this.length;
        var i = 0;
        var res = [];
        var newArr = [];
        for(; i<len; i++){
            if(i>=start && i<= start + length){
                res.push(this[i]);
                break;
    ...
    
  4. splice(start, num)
    Array.prototype.splice = function(start, num) {
      var additions = $A(arguments).slice(2);
      var delta = additions.length - num;
      var newlen = this.length + delta;
      if (delta >= 0) {
        this.length += delta;
      for (var i = start + num + delta; i < newlen + delta + 1; i++) {
        this[i] = this[i - delta];
    ...
    
  5. splice2(start, count, arr)
    Array.prototype.splice2 = function(start, count, arr) {
        this.splice(start, count);
        for (var i = 0; i < arr.length; ++i)
            this.splice(start + i, 0, arr[i]);