• Shuffles an array of plain JavaScript objects by the specified property.

    Remarks

    This function creates a shallow copy of the input array and sorts the resulting array by the specified property.

    Example

    Shuffle an array of objects by the "name" property

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];

    shuffleByProperty(people, 'name');
    // => { ok: true, value: [{ name: 'Bob', age: 25 }, { name: 'Charlie', age: 35 }, { name: 'Alice', age: 30 }] }

    Example

    Handle an invalid input:

    shuffleByProperty('not an array')
    // => { ok: false, value: error: Error('[collections.shuffleByProperty] Input is not an array') }

    Type Parameters

    • T

      The type of objects in the input array.

    • K extends string | number | symbol

      The type of the property used for sorting the input array.

    Parameters

    • arr: T[]

      The array to shuffle.

    • key: K

      The property to use for sorting the array.

    Returns Result<T[], Error>

    A Result object with either the shuffled array, or an Error object if the input is invalid.

Generated using TypeDoc v0.24.7