Javascript Array remove(numberToRemove)

Description

Javascript Array remove(numberToRemove)


Array.prototype.remove = function(numberToRemove) {
    var index,//from w  w  w . j  av  a 2s .c o  m
        len = this.length;

    for (index = 0; index < len; index += 1) {
        if (this[index] === numberToRemove) {
            this.splice(index, 1);
            index -= 1;
        }
    }
};

var arr = [1, 2, 5, 54, 32, 1, 4, 4, 1, 1, 1, 54, 3, 65, 4];

arr.remove(1);
console.log(arr);

Javascript Array remove(numberToRemove)

var numbers = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, "1"];

Array.prototype.remove = function (numberToRemove) {
    for (var i = 0; i < numbers.length; i++) {
        if (parseInt(numbers[i]) == numberToRemove) {
            numbers.splice(i, 1);//  w  w  w  .ja va2s .  co  m
            i--;
        }
    }
};

numbers.remove(1);
jsConsole.writeLine(numbers);



PreviousNext

Related