Remove by value function from array

A simple example of creating a fucntion for the array which will let you delete array members by value.
<!DOCTYPE html>
<html>
<head>
   
</head>
<body>

<script>
    
var exampleArray = ["A", "B", "C", "D"];
    
Array.prototype.removeVal = function(value) { 
    
// we have added a new function to the Array prototype so now we can easily delete any element using this added functionality
    
    for(var i=0; i < this.length; i++) {
        if(this[i] == value) {
            this.splice(i, 1);
            break;
        }
    }
}

console.log("The array BEFORE the removal of an element = " + exampleArray);

exampleArray.removeVal("C");
    
console.log("The array AFTER the removal of an element = " + exampleArray);

</script>

</body>
</html>

Responses

0 Replies