[JavaScript] How to find the first element of an array with a condition?


I want to find first element of an array with a condition, how can I do that in Javascript?

1 Answer

3 years ago by

arr.find(ele => condition) can be used to find the first element of an array which satisfies a particular condition. For example, below code shows you how to return the first element which is divisible by 5.

let arr = [1, 22, 35, 22, 1 , 5, 22, 1, 5];
console.log(arr.find(ele => ele%5 === 0));

Run here https://onecompiler.com/javascript/3xnkgptqx

3 years ago by Meera