Nodejs Array Splice splice(a, b)

Here you can find the source of splice(a, b)

Method Source Code

Array.prototype.splice = function(a, b)
{
  var tmp = [];//from   w  w w. j a v a2  s.  c  o  m
  for (var i = a + b; i < this.length; i++)
  {
    tmp[tmp.length] = this[i];
  }

  var rem = [];
  for (i = a; i < a + b; i++)
  {
    rem[rem.length] = this[i];
  }

  this.length = a;

  for (i = 2; i < arguments.length; i++)
  {
    this[this.length] = arguments[i];
  }

  for (i = 0; i < tmp.length; i++)
  {
    this[this.length] = tmp[i];
  }

  return rem;
};

Related

  1. 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;
    
  2. splice(start, deleteCount)
    Array.prototype.splice = function (start, deleteCount) {
       var max = Math.max,
          min = Math.min,
          delta,
          element,
          insertCount = max(arguments.length - 2, 0),
          k = 0,
          len = this.length,
          new_len,
    ...
    
  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];
    ...