Sometimes, we want to delete multiple items of an array by value with JavaScript.
In this article, we’ll look at how to delete multiple items of an array by value with JavaScript.
How to delete multiple items of an array by value with JavaScript?
To delete multiple items of an array by value with JavaScript, we can use the array filter
method.
For instance, we write:
const arr = [0, 1, 2, 3, 4];
const removal = 2;
const newArr = arr.filter((itm) => {
return itm !== removal
});
console.log(newArr)
to call arr.filter
to return an array without 2 in it.
To do this, we call filter
with a callback that returns itm !== removal
.
As a result, newArr
is [0, 1, 3, 4]
.
Conclusion
To delete multiple items of an array by value with JavaScript, we can use the array filter
method.