shuffle array elementsjavascript code example
Example 1: js shuffle array
yourArray.sort(function() { return 0.5 - Math.random() });
Example 2: how to shuffle an array javascript
/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = round(random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
shuffle(array);