Skip to content

replaceLastOr

Array.replaceLastOr(target, value, replacement, or)

Replaces the last occurrence of value with replacement in target array. If the value is not found, returns the fallback value or. If value and replacement are the same, returns the original array unchanged.

Example

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

Array.replaceLastOr([1, 2, 3, 2], 2, 5, []); // [1, 2, 3, 5]
Array.replaceLastOr([1, 2, 3], 4, 5, []); // []
ts
import { Array } from "@monstermann/array";

pipe([1, 2, 3, 2], Array.replaceLastOr(2, 5, [])); // [1, 2, 3, 5]
pipe([1, 2, 3], Array.replaceLastOr(4, 5, [])); // []