How do I remove a particular element from an array in JavaScript?
I have an array of integers, and I'm using the .push()
method to add elements to it.
Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(int);
.
I have to use core JavaScript - no frameworks are allowed.
Find the index
of the array element you want to remove, then remove that index with splice
.
var array = [2, 5, 9];
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
// array = [2, 9]
The second parameter of splice
is the number of elements to remove. Note that splice
modifies the array in place and returns a new array containing the elements that have been removed.
Note : browser support for indexOf is limited; it is not supported in Internet Explorer 7 and 8.
If you need indexOf
in an unsupported browser, try the following polyfill
. Find more info about this polyfill
here .
Array.prototype.indexOf || (Array.prototype.indexOf = function(d, e) {
var a;
if (null == this) throw new TypeError('"this" is null or not defined');
var c = Object(this),
b = c.length >>> 0;
if (0 === b) return -1;
a = +e || 0;
Infinity === Math.abs(a) && (a = 0);
if (a >= b) return -1;
for (a = Math.max(0 <= a ? a : b - Math.abs(a), 0); a < b;) {
if (a in c && c[a] === d) return a;
a++
}
return -1
});
I don't know how you are expecting array.remove(int)
to behave. There are three possibilities I can think of that you might be wanting.
To remove an element of an array at an index i
:
array.splice(i, 1);
If you want to remove every element with value number
from the array:
for(var i = array.length - 1; i >= 0; i--) {
if(array[i] === number) {
array.splice(i, 1);
}
}
If you just want to make the element at index i
no longer exist, but you don't want the indexes of the other elements to change:
delete array[i];
Edited on 2016 october
In this code example I use "array.filter(...)" function to remove unwanted items from array, this function doesn't change the original array and creates a new one. If your browser don't support this function (eg IE before version 9, or Firefox before version 1.5), consider using the filter polyfill from Mozilla .
Removing item (ECMA-262 Edition 5 code aka oldstyle JS)
var value = 3
var arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(function(item) {
return item !== value
})
console.log(arr)
// [ 1, 2, 4, 5 ]
Removing item (ES2015 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 ]
IMPORTANT ES2015 "() => {}" arrow function syntax is not supported in IE at all, Chrome before 45 version, Firefox before 22 version, Safari before 10 version. To use ES2015 syntax in old browsers you can use BabelJS
Removing multiple items (ES2016 code)
An additional advantage of this method is that you can remove multiple items
let forDeletion = [2, 3, 5]
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!
console.log(arr)
// [ 1, 4 ]
IMPORTANT "array.includes(...)" function is not supported in IE at all, Chrome before 47 version, Firefox before 43 version, Safari before 9 version and Edge before 14 version so here is polyfill from Mozilla
Removing multiple items (Cutting-edge experimental JavaScript ES2018?)
// array-lib.js
export function remove(...forDeletion) {
return this.filter(item => !forDeletion.includes(item))
}
// main.js
import { remove } from './array-lib.js'
let arr = [1, 2, 3, 4, 5, 3]
// :: This-Binding Syntax Proposal
// using "remove" function as "virtual method"
// without extending Array.prototype
arr = arr::remove(2, 3, 5)
console.log(arr)
// [ 1, 4 ]
Try it yourself in BabelJS :)
Reference