I have created below function to delete a custom element from an array:
Array.prototype.removeElement=function(x){
var index = this.indexOf(x);
if (index !== -1) {
this.splice(index, 1);
}
};
It works fine with below array:
var data = [1,2,3,4];
data.removeElement(2); //returns [1,3,4]
But when I have more than one item from a certain element it removes only first occurrence.
var data = [1,2,3,4,2];
data.removeElement(2);
// returns [1,3,4,2] while I expect to get [1,3,4]
I know I can do this by using loops, But I am curious to know if there is any cleaner code?