Javascript Array removeItem(item) method

Description

Javascript Array removeItem(item) method


Array.prototype.removeItem = (function (item){
    var newArray = [];
    for (var index in this) {
        if(this[index] !== item && typeof this[index] !== "function") {
            newArray.push(this[index]);//from w ww.  j a va2s .c  om
        }
    }

    return newArray;
});

var arr1 = [1, 2, 3, 2];
console.log(arr1.removeItem(3));

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

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

Javascript Array removeItem( value )


Array.prototype.removeItem = function( value ){
    for (var i = 0; i<this.length; i++) {
        if (this[i] === value) {
            this.splice(i, 1);// w  w  w.j  a v  a  2 s  . c  o  m
            i--;
        }
    };
}

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



PreviousNext

Related