Sometimes, we want to remove array element on condition with JavaScript.
In this article, we’ll look at how to remove array element on condition with JavaScript.
How to remove array element on condition with JavaScript?
To remove array element on condition with JavaScript, we can use the array filter
method.
For instance, we write
let ar = [1, 2, 3, 4];
ar = ar.filter((item) => !(item > 3));
to call ar.filter
with a callback to filter out all the items that are bigger than 3.
Then we assign the returned array back to ar
.
Therefore, the new value of ar
is [1, 2, 3]
.
Conclusion
To remove array element on condition with JavaScript, we can use the array filter
method.