Categories
JavaScript Answers

How to get array element fulfilling a condition with JavaScript?

Spread the love

To get array element fulfilling a condition with JavaScript, we call the filter method.

For instance, we write

const isGreaterThanFive = (x) => {
  return x > 5;
};

const filtered = [1, 10, 4, 6].filter(isGreaterThanFive);

to call [1, 10, 4, 6].filter with isGreaterThanFive to return an array with the entries that are greater than 5 in the [1, 10, 4, 6] array.

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 *