Remove all instances of a value from an array in JavaScript
This post will discuss how to remove all instances of a given value from an array in JavaScript.
1. Using Array.prototype.filter() function
The recommended solution in JavaScript is to use the filter() method, which creates a new array with elements that pass the predicate function. The following code example shows how to remove all instances of specified values from the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
const arr = [1, 3, 5, 3, 7]; const value = 3; const result = arr.filter(function(x) { return x !== value; }); console.log(result); /* Output: [ 1, 5, 7 ] */ |
Note that this solution doesn’t modify the original array but creates a new array. You can further shorten the code with ES6 arrow functions.
|
1 2 3 4 5 6 7 8 9 |
const arr = [1, 3, 5, 3, 7]; const value = 3; const result = arr.filter(x => x !== value); console.log(result); /* Output: [ 1, 5, 7 ] */ |
2. Using jQuery
Similar to the JavaScript native filter() method, jQuery Library has the grep() method. Following is a simple example demonstrating usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); const $ = require("jquery")(window); const arr = [1, 3, 5, 3, 7]; const value = 3; const result = $.grep(arr, function(x) { return x !== value; }); console.log(result); /* Output: [ 1, 5, 7 ] */ |
3. Using Underscore/Lodash Library
The Underscore and Lodash library have the _.without method, which returns a copy of an array excluding the specified values. Here’s a simple example that removes every instance of the given value from an array with the _.without method.
|
1 2 3 4 5 6 7 8 9 10 11 |
const _ = require('lodash'); const arr = [1, 3, 5, 3, 7]; const value = 3; const result = _.without(arr, value); console.log(result); /* Output: [ 1, 5, 7 ] */ |
4. Using Array.prototype.splice() function
The splice() method in JavaScript is often used to in-place add or remove elements from an array. The idea is to find indexes of all the elements to be removed from an array and then remove each element from the array using the splice() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Array.prototype.remove = function(value) { for (var i = this.length; i--; ) { if (this[i] === value) { this.splice(i, 1); } } } const arr = [1, 3, 5, 3, 7]; const value = 3; arr.remove(value); console.log(arr); /* Output: [ 1, 5, 7 ] */ |
5. Using delete operator
The JavaScript delete operator removes a property from an object. If called on an array arr like delete arr[i], it replaces the value present at index i with a value undefined. In other words, it leaves an empty slot at index i. This would translate to a simple code below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Array.prototype.remove = function(value) { for (var i = this.length; i--; ) { if (this[i] === value) { delete arr[i]; } } } const arr = [1, 3, 5, 3, 7]; const value = 3; arr.remove(value); console.log(arr); /* Output: [ 1, <1 empty item>, 5, <1 empty item>, 7 ] */ |
That’s all about removing all instances of a value from an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)