Function firstOrDefault

  • Returns the first element in the source that satisfies the predicate or a default value.

    Type Parameters

    • T

      Type of items in the source.

    Parameters

    • defaultValue: ValueOrFactory<T>

      Default value or factory to use if no element satisfies the predicate.

    • predicate: PredicateFunction<T> = ...

      Predicate function to determine if an element should be returned.

    Returns CollectorFunction<T, T>

    Collector that returns the first element in the source that satisfies the predicate or a default value.

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