Javascript Array removeItem(input)

Description

Javascript Array removeItem(input)


Array.prototype.removeItem = function removeItem(input) {
  
  for (var i = 0; i < this.length; i++) {
    var index = this.indexOf(input);
    if (index > -1) {
      this.splice(index, 1);//from  w  w w  .  ja v  a  2 s .co m
    }
  }
  return this;
};

var arr1 = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr1.removeItem(1);

var arr2 = ['hi', 'bye', 'hello'];
arr2.removeItem('bye');

Javascript Array RemoveItem(input)

Array.prototype.RemoveItem = function(input){
    for(var cnt = 0; cnt<this.length; cnt++){
        if(this[cnt] === input){
            this.splice(cnt, 1);/*ww w.  j av a  2s  .c  o m*/
            cnt--;
        }
    }
    return this;
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log(arr.RemoveItem(1));
arr = ['hi', 'bye', 'hello' ];
console.log(arr.RemoveItem('bye'));



PreviousNext

Related