Skip to content

randomOrElse

Array.randomOrElse(array, callback)

Returns a random element from array, or the result of calling callback with the array if the array is empty.

Example

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

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

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