Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Created February 27, 2024 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsmikita/8c8f388231e86cb81555b0009a325e65 to your computer and use it in GitHub Desktop.
Save itsmikita/8c8f388231e86cb81555b0009a325e65 to your computer and use it in GitHub Desktop.
Shuffle JavaScript Object
/**
* Shuffle Object
*
* The Schwartzian Transform way @link https://en.wikipedia.org/wiki/Schwartzian_transform
* Stolen from @link https://stackoverflow.com/a/46545530
*
* @param {object|Array} unshuffled
*/
export const shuffle = unshuffled => unshuffled
.map(value => ({value, sort: Math.random()}))
.sort((a, b) => a.sort - b.sort)
.map(({value}) => value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment