Skip to content

Instantly share code, notes, and snippets.

@robertoentringer
Forked from guilhermepontes/shuffle.js
Created May 30, 2018 10:33
Show Gist options
  • Save robertoentringer/bb6338b6cdea8daa5241f00ece285ea8 to your computer and use it in GitHub Desktop.
Save robertoentringer/bb6338b6cdea8daa5241f00ece285ea8 to your computer and use it in GitHub Desktop.
Shuffle array element ES2015, ES6
// original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
// fully random by @BetonMAN
const shuffleArray = arr => (
arr
.map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map(a => a[1]);
);
shuffleArray([1, 2, 3]) //[3, 1, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment