Javascript Array remove(numb)

Description

Javascript Array remove(numb)


//Problem 2. Remove elements
//Write a function that removes all elements with a given value.
//    Attach it to the array type.
//    Read about prototype and how to attach methods.


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

var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1'];

arr.remove(1);

console.log(arr);



PreviousNext

Related