Function lastOrDefault

  • Returns the last element in the source that satisfies the predicate or a default value if no such element is found.

    Type Parameters

    • T

      Type of items in the source.

    Parameters

    • defaultValue: ValueOrFactory<T>

      Default value to return if no element is found.

    • predicate: PredicateFunction<T> = ...

      Predicate function to determine if an element should be returned.

    Returns CollectorFunction<T, T | undefined>

    Collector that returns the last element in the source that satisfies the predicate or a default value if no such element is found.

    const result = lastOrDefault<number>(0, (x) => x > 1)([1, 2, 3]);
    console.log(result); // 3