How to remove item from array by value?
How to remove item from array by value?
Question
Is there a method to remove an item from a JavaScript array?
Given an array:
var ary = ['three', 'seven', 'eleven'];
I would like to do something like:
removeItem('seven', ary);
I've looked into splice()
but that only removes by the position number, whereas I need something to remove an item by its value.
Accepted Answer
This can be a global function or a method of a custom object, if you aren't allowed to add to native prototypes. It removes all of the items from the array that match any of the arguments.
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
var ary = ['three', 'seven', 'eleven'];
ary.remove('seven');
/* returned value: (Array)
three,eleven
*/
To make it a global-
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
/* returned value: (Array)
three,eleven
*/
And to take care of IE8 and below-
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(what, i) {
i = i || 0;
var L = this.length;
while (i < L) {
if(this[i] === what) return i;
++i;
}
return -1;
};
}
Popular Answer
You can use the indexOf
method like this:
var index = array.indexOf(item);
array.splice(index, 1);
var array = [1,2,3,4]
var item = 3
var index = array.indexOf(item);
array.splice(index, 1);
console.log(array)
Read more… Read less…
A one-liner will do it,
var ary = ['three', 'seven', 'eleven'];
// Remove item 'seven' from array
var filteredAry = ary.filter(function(e) { return e !== 'seven' })
//=> ["three", "eleven"]
// In ECMA6 (arrow function syntax):
var filteredAry = ary.filter(e => e !== 'seven')
This makes use of the filter function in JS. It's supported in IE9 and up.
What it does (from the doc link)
filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are simply skipped, and are not included in the new array.
So basically, this is the same as all the other for (var key in ary) { ... }
solutions, except that the for in
construct is supported as of IE6.
Basically, filter is a convenience method that looks a lot nicer (and is chainable) as opposed to the for in
construct (AFAIK).
You can use underscore.js. It really makes things simple.
For example, with this:
var result = _.without(['three','seven','eleven'], 'seven');
And result
will be ['three','eleven']
.
In your case the code that you will have to write is:
ary = _.without(ary, 'seven')
It reduces the code that you write.
You can do it with these two ways:
const arr = ['1', '2', '3', '4'] // we wanna delete number "3"
The first way:
arr.indexOf('3') !== -1 && arr.splice(arr.indexOf('3'), 1)
The second way (ES6) specially without mutate:
const newArr = arr.filter(e => e !== '3')
Check out this way:
for(var i in array){
if(array[i]=='seven'){
array.splice(i,1);
break;
}
}
and in a function:
function removeItem(array, item){
for(var i in array){
if(array[i]==item){
array.splice(i,1);
break;
}
}
}
removeItem(array, 'seven');
The simplest solution is:
array - array for remove some element valueForRemove; valueForRemove - element for remove;
array.filter(arrayItem => !array.includes(valueForRemove));
More simple:
array.filter(arrayItem => arrayItem !== valueForRemove);
No pretty, but works:
array.filter(arrayItem => array.indexOf(arrayItem) != array.indexOf(valueForRemove))
No pretty, but works:
while(array.indexOf(valueForRemove) !== -1) {
array.splice(array.indexOf(valueForRemove), 1)
}
P.S. The filter() method creates a new array with all elements that pass the test implemented by the provided function. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter