Remarks
- This function removes every occurence of specified value from array.
- Function name have "stackoverflow_" prefix to prevent name collision. If you accepts the risk of name collision, you can remove that prefix.
- There are described 4 versions of this function for different cases.
Option #1 Extending "Array.prototype" with "Object.defineProperty" function
Compatible browsers: Internet Explorer 9+, Firefox 4+, Chrome 5+, Safari 5+, and Opera 12+
Extend the Array prototype by using "Object.defineProperty" function.
This approach will not cause problems with enumeration, because we marked "enumerable" as "false".
Be sure that your browser supports "Object.defineProperty" function. Here is the compatibility table:
http://kangax.github.io/es5-compat-table/#Object.defineProperty
Extension code:
// Extending Array prototype with new function,
// if that function is already defined in "Array.prototype",
// then "Object.defineProperty" will throw an exception
Object.defineProperty(Array.prototype, "stackoverflow_remove", {
// Specify "enumerable" as "false" to prevent function enumeration
enumerable: false,
/**
* Removes all occurence of specified item from array
* @this Array
* @param itemToRemove Item to remove from array
* @returns {Number} Count of removed items
*/
value: function (itemToRemove) {
// Count of removed items
var removeCounter = 0;
// Iterate every array item
for (var index = 0; index < this.length; index++) {
// If current array item equals itemToRemove then
if (this[index] === itemToRemove) {
// Remove array item at current index
this.splice(index, 1);
// Increment count of removed items
removeCounter++;
// Decrement index to iterate current position
// one more time, because we just removed item
// that occupies it, and next item took it place
index--;
}
}
// Return count of removed items
return removeCounter;
}
});
Usage code #1:
var arr = [1, 2, 3, 2, 2, 2];
var itemsRemoved = arr.stackoverflow_remove(2);
console.log(itemsRemoved);
// 4
console.log(arr);
// [1, 3]
Usage code #2:
var arr = ["tree", "bird", "car", "bird", "bird"];
var itemsRemoved = arr.stackoverflow_remove("bird");
console.log(itemsRemoved);
// 3
console.log(arr);
// ["tree", "car"]
Option #2 Defining global function. For old browsers which not support prototype extending with "Object.defineProperty"
If you want to use this function without "Object.defineProperty", you can define it as a global scope function.
Extension code:
/**
* Removes all occurence of specified item from array
* @param array Array
* @param itemToRemove Item to remove from array
* @returns {Number} Count of removed items
*/
function stackoverflow_removeArrayItem(array, itemToRemove) {
// Count of removed items
var removeCounter = 0;
// Iterate every array item
for (var index = 0; index < array.length; index++) {
// If current array item equals itemToRemove then
if (array[index] === itemToRemove) {
// Remove array item at current index
array.splice(index, 1);
// Increment count of removed items
removeCounter++;
// Decrement index to iterate current position
// one more time, because we just removed item
// that occupies it, and next item took it place
index--;
}
}
// Return count of removed items
return removeCounter;
}
Usage code:
var arr = ["tree", "bird", "car", "bird", "bird"];
var itemsRemoved = stackoverflow_removeArrayItem(arr, "bird");
console.log(itemsRemoved);
// 3
console.log(arr);
// ["tree", "car"]
Option #3 For high performance
This code uses a "filter" function and it works about 50 times faster than previous options, but this approach creates new array.
Extension code:
// Extending Array prototype with new function,
// if that function is already defined in "Array.prototype",
// then "Object.defineProperty" will throw an exception
Object.defineProperty(Array.prototype, "stackoverflow_filterValue", {
// Specify "enumerable" as "false" to prevent function enumeration
enumerable: false,
/**
* Create new array where specified item is removed
* @this Array
* @param itemToRemove Item to remove from array
* @returns {Number} Count of removed items
*/
value: function (itemToRemove) {
var filteredArray = this.filter(function(item){
return item !== itemToRemove;
});
return filteredArray;
}
});
Usage code:
var arr = [1, 2, 3, 2, 2, 2];
// PAY ATTENTION.
// Original array stay unchanged.
var filteredArray = arr.stackoverflow_filterValue(2);
console.log(filteredArray);
// [1, 3]
Option #4 ECMAScript 2015 way (if your browser support modern JavaScript or you use Babel.js)
Using new version of JavaScript we need no custom functions to remove array items. Using only filter(...) function and arrow function we got very tiny code:
let value = 3;
let arr = [1, 2, 3, 4, 5, 3];
arr = arr.filter(item => item !== value);
console.log(arr);
// [ 1, 2, 4, 5 ]
indexOf
in IE. – Scotty.NET Sep 13 '13 at 7:48_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
– zhon Sep 27 '13 at 20:42