Skip to content

lastIndexOfOrElse

ts
function Array.lastIndexOfOrElse<T, U>(
    target: readonly T[],
    value: NoInfer<T>,
    orElse: (target: readonly NoInfer<T>[]) => U,
): number | U

Returns the index of the last occurrence of value in target. If value is not found, calls orElse with the original array.

Example

ts
import { Array } from "@monstermann/array";

Array.lastIndexOfOrElse([1, 2, 3, 2], 2, () => -1); // 3
Array.lastIndexOfOrElse([1, 2, 3], 4, (arr) => arr.length); // 3
ts
import { Array } from "@monstermann/array";

pipe(
    [1, 2, 3, 2],
    Array.lastIndexOfOrElse(2, () => -1),
); // 3

pipe(
    [1, 2, 3],
    Array.lastIndexOfOrElse(4, (arr) => arr.length),
); // 3