Skip to content

Instantly share code, notes, and snippets.

@exdeniz
Forked from guilhermepontes/shuffle.js
Created June 4, 2018 08:36
Show Gist options
  • Save exdeniz/2c7fb8de0f852d96aef3e3ed5fa366d5 to your computer and use it in GitHub Desktop.
Save exdeniz/2c7fb8de0f852d96aef3e3ed5fa366d5 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