A function to execute on each value in the array, returning
true
when the item in the array matches the condition. The
signature for predicate
is identical to the signature for
the first argument to Array.prototype.find
. The function
is called once for each element of the array, in ascending
order, until it finds one where predicate returns true. If
such an element is found, find immediately returns that
element value wrapped in Just
. Otherwise, Maybe.find
returns Nothing
.
*
The array to search using the predicate.
Safely search for an element in an array.
This function behaves like
Array.prototype.find
, but returnsMaybe<T>
instead ofT | undefined
.Examples
The basic form is:
The function is curried so you can use it in a functional chain. For example (leaving aside error handling on a bad response for simplicity), suppose the url
https://arrays.example.com
returned a JSON payload with the typeArray<{ count: number, name: string }>
, and we wanted to get the first of these wherecount
was at least 100. We could write this: