Finding elements in Arrays and TypedArrays · V8

submited by
Style Pass
2021-10-27 19:00:18

Finding an element that satisfies some condition in an Array is a common task and is done with the find and findIndex methods on Array.prototype and the various TypedArray prototypes. Array.prototype.find takes a predicate and returns the first element in the array for which that predicate returns true. If the predicate doesn't return true for any element, the method returns undefined.const inputArray = [{v:1}, {v:2}, {v:3}, {v:4}, {v:5}];
inputArray.find((element) => element.v % 2 === 0);
// → {v:2}
inputArray.find((element) => element.v % 7 === 0);
// → undefined

Array.prototype.findIndex works similarly, except it returns the index when found, and -1 when not found. The TypedArray versions of find and findIndex work exactly the same, with the only difference being that they operate on TypedArray instances instead of Array instances.inputArray.findIndex((element) => element.v % 2 === 0);
// → 1
inputArray.findIndex((element) => element.v % 7 === 0);
// → -1Finding elements from the end #

Leave a Comment