Reference / JavaScript
Array.prototype.find()
Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
Syntax
find(predicate: (element: T, index: number, array: T[]) => boolean): T | undefinedParameters
| Name | Type | Description |
|---|---|---|
| predicate | Function | Function executed for each value. |
Examples
Find by ID
const users = [{ id: 10, name: 'Alice' }, { id: 20, name: 'Bob' }];
const found = users.find(u => u.id === 20);
console.log(found); // { id: 20, name: 'Bob' }Stops iterating as soon as a matching element is found (short-circuits).