ES6 - Array.prototype.find



find lets you iterate through an array and get the first element back that causes the given callback function to return true.

. Once an element has been found, the function immediately returns. Its an efficient way to get at just the first item that matches a given condition.

Example

var numbers = [1, 2, 3]; 
var oddNumber = numbers.find((x) => x % 2 == 1); 
console.log(oddNumber); // 1

Output

1

Note − The ES5 filter() and the ES6 find() are not synonymous. Filter always returns an array of matches (and will return multiple matches), find always returns the actual element.

Advertisements