Nodejs Array Remove by Index removeAt( index )

Here you can find the source of removeAt( index )

Method Source Code

// add removeAt for array
Array.prototype.removeAt = function( index ){
   // 0-based index
   if(index>=0 && index<this.length){
      for(var i=index; i<this.length; i++){
         this[i] = this[i+1];//from w  w w.  java 2 s  .  c om
      }
      this.length = this.length-1;
   }
   return this;
}

function removeElement(index,array){
   if(index>=0 && index<array.length){
      for(var i=index; i<array.length; i++){
         array[i] = array[i+1];
      }
      array.length = array.length-1;
   }
   return array;
}

Related

  1. remove(index)
    Array.prototype.remove=function(index){
      for(var i=0;i<this.length;i++){
        if(i==index){
          this.splice(i, 1);
          break;
    
  2. remove(n)
    Array.prototype.remove = function(n) {
      this.splice(this.indexOf(n), 1);
    };
    
  3. remove(n)
    Array.prototype.remove = function(n){
      for(var i in this){
        if(this[i]==n){
          this.splice(i,1);
          break;
      return this;
    
  4. remove(pos)
    Array.prototype.remove = function (pos) {
        this.splice(pos, 1);
        return this;
    };
    
  5. remove(pred)
    Array.prototype.remove = function(pred) {
      var removed = this.filter(function(item) {
        return pred(item)
      })
      this.forEach(function(i, index, arr) {
        if (removed.indexOf(i) !== -1) arr.splice(index, 1)
      })
      return removed
    var arr = [1, 2, 3, 4, 5, 6]
    var removed = arr.remove(function(e) {return e % 2 === 0})
    console.log(arr)
    console.log(removed)
    
  6. removeAt( index )
    Array.prototype.removeAt = function( index )
      var part1 = this.slice( 0, index );
      var part2 = this.slice( index );
      part1.pop();
      return( part1.concat( part2 ) );
    
  7. removeAt(index)
    Array.prototype.removeAt = function (index)
        if (this.length <= index || index < 0)
            return;
        for (var i = index + 1, n = index; i < this.length; ++i, ++n)
            this[n] = this[i];
    ...
    
  8. removeAt(index)
    Array.prototype.removeAt = function(index) {
        this.splice(index, 1);
    
  9. removeAt(index)
    "use strict";
    Array.prototype.removeAt = function(index) {
        this.splice(index, 1);
        return this;
    };
    Array.prototype.print = function() {
        for (var i = 0; i < this.length; i++) {
            console.log(this[i] + ", ");
    };
    Array.prototype.remove = function() {
        var what, a = arguments,
            L = a.length,
            ax;
        while (L && this.length) {
            what = a[--L];
            while ((ax = this.indexOf(what)) !== -1) {
                this.splice(ax, 1);
        return this;
    };