Nodejs Array Remove by Index removeAt(index)

Here you can find the source of removeAt(index)

Method Source Code

// Given an array and an index into the array, remove and return the array value at that index.

Array.prototype.removeAt = function(index) {
   index = parseInt(Math.floor(index));
   if (Number.isNaN(index)){
      return undefined;
   }/*ww  w.j a va2  s .  c o m*/
   var temp = this[index];

   for (var i = index; i < this.length-1; i++) {
      this[i] = this[i+1];
   }
   if (index <= this.length) {
      this.length = this.length-1;
   }
   return temp;
}

// test cases

//successful case
var array = [0,1,2];
var removedValue = array.removeAt(0);
console.log(array);
console.log(removedValue);

//removing at an index that is not an integer and outside the array
array = [0,1,2];
removedValue = array.removeAt(4.4);
console.log(array);
console.log(removedValue);

//removing at an index that is not a number
array = [0,1,2];
removedValue = array.removeAt("A");
console.log(array);
console.log(removedValue);

Related

  1. removeAt( index )
    Array.prototype.removeAt = function( index ){
      if(index>=0 && index<this.length){
        for(var i=index; i<this.length; i++){
          this[i] = this[i+1];
        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;
    
  2. removeAt( index )
    Array.prototype.removeAt = function( index )
      var part1 = this.slice( 0, index );
      var part2 = this.slice( index );
      part1.pop();
      return( part1.concat( part2 ) );
    
  3. 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];
    ...
    
  4. removeAt(index)
    Array.prototype.removeAt = function(index) {
        this.splice(index, 1);
    
  5. 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;
    };
    
  6. removeAt(index)
    Array.prototype.removeAt=function(index){
      this.splice(index,1);
    
  7. removeAt(index)
    Array.prototype.removeAt = function(index) {
        if (index > this.length - 1 || index < 0) {
            throw new Error("Index out of range");
        this.splice(index, 1);
    };
    
  8. removeAt(position)
    Array.prototype.removeAt = function(position){
        this.splice(position,1);
    };
    
  9. removeAtIndex(elementAt)
    Array.prototype.removeAtIndex = function(elementAt) {
      return this[elementAt - 1];
    };