1. 简单的利用sort进行打乱排序
arr.sort(function(){ return .5 - Math.random() })
2. 洗牌算法打乱排序
function shuffle(arr) { let i = arr.length; while (i) { let j = Math.floor(Math.random() * i--); [arr[j], arr[i]] = [arr[i], arr[j]] } console.log(arr) }
原文:https://www.cnblogs.com/wangyisu/p/13752188.html