• Find the first value in an iterable which matches a predicate.

    Type Parameters

    • T

    Parameters

    • object: Iterable<T>

      The iterable object to search.

    • fn: ((value, index) => boolean)

      The predicate function to apply to the values.

        • (value, index): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    Returns T | undefined

    The first matching value, or undefined if no matching value is found.

    Complexity

    Linear.

    Example

    import { find } from '@lumino/algorithm';

    interface IAnimal { species: string, name: string };

    function isCat(value: IAnimal): boolean {
    return value.species === 'cat';
    }

    let data: IAnimal[] = [
    { species: 'dog', name: 'spot' },
    { species: 'cat', name: 'fluffy' },
    { species: 'alligator', name: 'pocho' }
    ];

    find(data, isCat).name; // 'fluffy'