Skip to content

Instantly share code, notes, and snippets.

@borgeslt
Last active February 3, 2020 01:28
Show Gist options
  • Save borgeslt/b63810b17d668c4f65ab63eb8c294250 to your computer and use it in GitHub Desktop.
Save borgeslt/b63810b17d668c4f65ab63eb8c294250 to your computer and use it in GitHub Desktop.
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