Categories
JavaScript Answers

How to remove array element on condition with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *