// 原始数组
let arr = [333, 222, 323, 232, 332, 223, 322, 233];
// 随机打乱数组顺序的函数
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// 打乱数组顺序
shuffle(arr);
let obj = [];
//1
obj.push(arr[Math.floor(Math.random() * arr.length)]);
//2
obj.push(arr[Math.floor(Math.random() * arr.length)]);
//3
obj.push(arr[Math.floor(Math.random() * arr.length)]);
//4
obj.push(arr[Math.floor(Math.random() * arr.length)]);
//5
obj.push(arr[Math.floor(Math.random() * arr.length)]);
//6
obj.push(arr[Math.floor(Math.random() * arr.length)]);
console.log(obj)
js 随机一象
于 2024-05-18 10:17:19 首次发布