Skip to content

Instantly share code, notes, and snippets.

@tjinlag
Created March 24, 2024 09:57
Show Gist options
  • Save tjinlag/2c36ce2c712d479cc7e466ecae45a2cf to your computer and use it in GitHub Desktop.
Save tjinlag/2c36ce2c712d479cc7e466ecae45a2cf to your computer and use it in GitHub Desktop.
Shuffle an array
const shuffleItems = (items) => {
return items.map(item => ({ sort: Math.random(), value: item }))
.sort((item1, item2) => item1.sort - item2.sort)
.map(item => item.value);
}
shuffleItems([1,2,3,4,5,6]);
shuffleItems([1,2,3,4,5,6,6,8,10,12]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment