• Returns a random element or an array of random elements from an input array without modifying the original array. Only elements of type number, string, or boolean are considered valid.

    Example

    const input1 = [1, 2, 3, 4, 5];
    const result1 = pickRandom(input1); // ok
    const result2 = pickRandom(input1, 2); // ok

    const input3 = ["apple", "banana", "cherry", "date"];
    const result3 = pickRandom(input3, 3); // ok

    const input4 = [true, false, undefined, null];
    const result4 = pickRandom(input4, 1); // err

    const input5 = [1, 2, 3, 4, 5];
    const result5 = pickRandom(input5, 3); // ok

    const input6 = ["a", "b", "c", "d", "e"];
    const result6 = pickRandom(input6, 2); // ok

    Type Parameters

    • T extends string | number | boolean

      The type of the elements in the input array.

    Parameters

    • arr: T[]

      The input array from which to select random elements.

    • quantity: number = 1

      Optional. The number of random elements to return. Defaults to 1.

    Returns Result<T | T[], Error>

    A Result object with either the selected random element(s), or an Error object if the provided input is not an array.

Generated using TypeDoc v0.24.7