Standard Toolkit
Packages@accelint/coreArray

find

Returns the first element of the given array that satisfies the predicate, or null if not found.

Usage

import { find } from '@accelint/core/array/find';

// Basic usage - find first even number
const firstEven = find((x: number) => x % 2 === 0)([1, 2, 3, 4, 5]);
// 2

// Not found returns null
const notFound = find((x: number) => x > 10)([1, 2, 3]);
// null

Reference

function find<T>(
  predicate: (element: T, index: number) => boolean
): (arr: T[]) => T | null

Parameters

ParameterTypeDescription
predicate(element: T, index: number) => booleanFunction called for each element until one passes. Receives the element and its index.

Returns

Returns a curried function that accepts an array and returns the first element that satisfies the predicate, or null if none found.

Type Parameters

  • T - The type of elements in the array

Examples

Example: Finding an object

import { find } from '@accelint/core/array/find';

interface User {
  id: number;
  name: string;
}

const users: User[] = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

const user = find((u: User) => u.id === 2)(users);
// { id: 2, name: 'Bob' }

Example: Using index parameter

import { find } from '@accelint/core/array/find';

// Find first element at an even index
const evenIndexed = find((_: string, index: number) => index % 2 === 0);
evenIndexed(['a', 'b', 'c', 'd']);
// 'a' (index 0)

Good to know: Returns null (not undefined) when no element matches. The predicate stops executing once a match is found, providing early exit optimization. This is a pure function with no side effects.

  • filter - Get all elements matching predicate
  • find-index - Get index of first matching element
  • find-last - Find from the end of the array
  • some - Test if any element passes predicate

On this page