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

    Function last

    • Returns the last 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 last element of the sequence is returned. When a type guard is supplied, the returned value is narrowed to TFiltered.

      Returns TFiltered

      The last element that satisfies the predicate (or the final element when no predicate is supplied).

      Thrown when the sequence is empty.

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

      The entire sequence is enumerated to locate the final match.

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

      const lastEven = last(numbers, x => x % 2 === 0);
      console.log(lastEven); // 4
    • Returns the last 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 final element of the sequence is returned.

      Returns TElement

      The last element that satisfies predicate, or the final element when no predicate is supplied.

      Thrown when the sequence is empty.

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

      The entire sequence is enumerated to locate the final match.