@mirei/ts-collections
    Preparing search index...

    Function first

    • Returns the first element in the sequence, optionally filtered by a predicate or type guard.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TFiltered

        Subtype confirmed when a type guard predicate is supplied.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • predicate: TypePredicate<TElement, TFiltered>

        Predicate evaluated against each element; when omitted, the first element is returned. When a type guard is supplied, the returned value is narrowed to TFiltered.

      Returns TFiltered

      The first element that satisfies the predicate (or the very first element when none is provided).

      Thrown when the sequence is empty.

      Thrown when a predicate is supplied and no element satisfies it.

      Enumeration stops immediately once a matching element is found.

      const numbers = [1, 2, 3, 4, 5];
      const firstElement = first(numbers);
      console.log(firstElement); // 1

      const firstEven = first(numbers, x => x % 2 === 0);
      console.log(firstEven); // 2
    • Returns the first element in the sequence that satisfies an optional predicate.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalpredicate: Predicate<TElement>

        Optional predicate evaluated against each element; when omitted, the first element is returned.

      Returns TElement

      The first element that satisfies predicate, or the very first element when the predicate is missing.

      Thrown when the sequence is empty.

      Thrown when a predicate is supplied and no element satisfies it.

      Enumeration stops immediately once a matching element is found.