Skip to main content

findFirst

Selects the first item from an array, collection or enumerable object that matches the truth function.

Signature

findFirst<T>(
list: TList<T> | undefined,
fn: (item: T) => boolean
): T | undefined

Parameters

NameTypeOptionalDescription
listT[] | undefinedNoSpecifies the array, collection or enumerable object to iterate through.
fn(item: T) => booleanNoSpecifies 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 or undefined if no item is found.

Example

import { findFirst } from "@tripetto/runner";

findFirst<number>([1, 2, 3], (nItem: number) => nItem > 1); // Returns `2`