Function find

  • Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

    Type Parameters

    • T

    Parameters

    • collection: ArrayLike<T>

      The collection to inspect.

    • predicate: ((item) => boolean)

      function invoked per iteration.

        • (item): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional fromIndex: number

      The index to search from.

      Optional

    Returns T | undefined

    Returns the first matched element, else undefined.

    Since

    5.2.0

    Example

    var users = [
    { 'user': 'barney', 'age': 36, 'active': true },
    { 'user': 'fred', 'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1, 'active': true }
    ];

    find(users, function(o) { return o.age < 40; });
    // => object for 'barney'

    // The `matches` iteratee shorthand.
    find(users, { 'age': 1, 'active': true });
    // => object for 'pebbles'

    // The `matchesProperty` iteratee shorthand.
    find(users, ['active', false]);
    // => object for 'fred'

    // The `property` iteratee shorthand.
    find(users, 'active');
    // => object for 'barney'
  • Type Parameters

    • T

    Parameters

    • collection: ArrayLike<T>
    • predicate: Partial<T>
    • Optional fromIndex: number
      Optional

    Returns T | undefined

  • Type Parameters

    • T

    Parameters

    • collection: ArrayLike<T>
    • predicate: (boolean | keyof T)[]
    • Optional fromIndex: number
      Optional

    Returns T | undefined

  • Type Parameters

    • T

    Parameters

    • collection: ArrayLike<T>
    • predicate: keyof T
    • Optional fromIndex: number
      Optional

    Returns T | undefined