Created
February 27, 2024 03:29
-
-
Save itsmikita/8c8f388231e86cb81555b0009a325e65 to your computer and use it in GitHub Desktop.
Shuffle JavaScript Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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