Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.