array.shuffle js code example
Example 1: js shuffle array
yourArray.sort(function() { return 0.5 - Math.random() });
Example 2: shuffle array javascript
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
var myArray = ['1','2','3','4','5','6','7','8','9'];
shuffle(myArray);