Skip to content

Instantly share code, notes, and snippets.

@juliovedovatto
Last active September 20, 2016 13:50
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 juliovedovatto/4f86b6800b5134cdf502ee3940798a4f to your computer and use it in GitHub Desktop.
Save juliovedovatto/4f86b6800b5134cdf502ee3940798a4f to your computer and use it in GitHub Desktop.
ECMAScript 6 Array shuffle
// METHOD 1
Object.assign(Array.prototype, {
shuffle() {
let m = this.length, i;
while (m) {
i = (Math.random() * m--) >>> 0;
[this[m], this[i]] = [this[i], this[m]];
}
return this;
}
});
// METHOD 2
Object.assign(Array.prototype, {
shuffle() {
return this.sort(() => 0.5 - Math.random());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment