Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shuffling an array in Javascript
Array.prototype.shuffle = function () {
return this.map(item => {
return {
index: Math.random(),
value: item
}
})
.sort((item1, item2) => item1.index - item2.index)
.map(item => item.value)
}
let arr = [
{ x:1, y:1 },
{ x:2, y:2 },
{ x:3, y:3 },
{ x:4, y:4 },
{ x:5, y:5 },
];
let shuffleArr = arr.shuffle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment