find
The find() method returns the first element of an array that satisfies a given condition.
Interactive example
Syntax
Classic signature
typescript
// Type Guard
function find<
GenericInput extends readonly unknown[],
GenericOutput extends GenericInput[number]
>(
input: GenericInput,
predicate: (
element: GenericInput[number],
params: ArrayFindParams
) => element is GenericOutput
): GenericOutput | undefined
// Boolean
function find<
GenericInput extends readonly unknown[]
>(
input: GenericInput,
predicate: (
element: GenericInput[number],
params: ArrayFindParams
) => boolean
): GenericInput[number] | undefinedCurried signature
typescript
// Type Guard
function find<
GenericInput extends readonly unknown[],
GenericOutput extends GenericInput[number]
>(
predicate: (
element: GenericInput[number],
params: ArrayFindParams
) => element is GenericOutput
): (input: GenericInput) => GenericOutput | undefined
// Boolean
function find<
GenericInput extends readonly unknown[]
>(
predicate: (
element: GenericInput[number],
params: ArrayFindParams
) => boolean
): (input: GenericInput) => GenericInput[number] | undefinedHelper parameters
typescript
interface ArrayFindParams {
index: number;
}Parameters
input: The array to search in.predicate: Predicate function that tests each element (and can act as a type guard). Receives the element and an object containing the index.params.index: Position of the current element in the array.
Return value
The first element that satisfies the condition, or undefined if no element matches.
See also
findLast- Finds the last element that satisfies a conditionfindIndex- Returns the index instead of the element
