Creates a function that returns the first element in an iterable that satisfies the predicate. Similar to Array.prototype.find(), but works with any iterable and throws an error if no match is found.
Type of elements in the iterable
Function to test each element and its index
A function that takes an iterable and returns the first matching element
Error if no element passes the predicate test
// Find first even numberconst firstEven = find((n: number) => n % 2 === 0)([1, 3, 4, 5, 6])// Result: 4 Copy
// Find first even numberconst firstEven = find((n: number) => n % 2 === 0)([1, 3, 4, 5, 6])// Result: 4
// This would throw "Not found." errortry { const firstNegative = find((n: number) => n < 0)([1, 2, 3, 4])} catch (error) { // Error: "Not found."} Copy
// This would throw "Not found." errortry { const firstNegative = find((n: number) => n < 0)([1, 2, 3, 4])} catch (error) { // Error: "Not found."}
maybeFind - For a non-throwing variant that returns undefined if not found
Creates a function that returns the first element in an iterable that satisfies the predicate. Similar to Array.prototype.find(), but works with any iterable and throws an error if no match is found.