To remove a specific item from an array with JavaScript, we call the array splice
method.
For instance, we write
const array = [2, 5, 9];
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array);
to get the index of 5 in array
with indexOf
.
Then we call splice
with index
and 1 to remove 5 from the array.