使用现代版本的Fisher-Yates随机播放算法:
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;}ES2015(ES6)版本
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);



