find
Finds the first item from an array, collection or enumerable object that matches the truth function and returns its key (that is the zero-based index number of an item in an array or the property name in an object).
Signature
find<T>(
list: TList<T> | undefined,
fn: (item: T) => boolean
): string | number | undefined
Parameters
Name | Type | Optional | Description |
---|---|---|---|
list | T[] | undefined | No | Specifies the array, collection or enumerable object to iterate through. |
fn | (item: T) => boolean | No | Specifies the truth function. Each item is supplied to the function, passing the item as argument. The function should return a boolean value whether to include the item in the selection. |
Return value
Returns the item key or undefined
if no item is found.
Example
import { find } from "@tripetto/builder";
find<number>([1, 2, 3], (nItem: number) => nItem > 1); // Returns `1`
find<Object>({a: 1, b: 2, c: 3}, (nItem: number) => nItem > 1); // Returns `b`