Last active
February 3, 2020 01:28
-
-
Save borgeslt/b63810b17d668c4f65ab63eb8c294250 to your computer and use it in GitHub Desktop.
Shuffling an array in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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