If you find yourself needing to eliminate a value that appears multiple times in an array (e.g. [1, 2, 2, 2, 4, 5, 6]).
function removeFromArray(arr, element) {
return arr.filter(e => e !== element);
};
var exampleArr = [1, 2, 3, 4, 5];
removeFromArray(exampleArr, 3);
// result will be
//[1, 2, 4, 5]
While you can use splice to remove a single element from the array, it's not effective for removing multiple occurrences of the same element.
function removeSingleValue(arr, val){
var index = arr.indexOf(val);
if (index > -1) arr.splice(index, 1);
return arr;
}
var exampleArr = [1, 2, 3, 4, 5, 5];
removeSingleValue(exampleArr, 5);
// result will be
//[1, 2, 3, 4, 5]