var arr = [1,2,3,4,5];
function randomsort(a, b) {
return Math.random()>.5 ? -1 : 1; //通过随机产生0到1的数,然后判断是否大于0.5从而影响排序,产生随机性的效果。
}
arr.sort(randomsort);
console.log(arr);
js/JavaScript数组随机排序
最新推荐文章于 2024-10-18 17:51:23 发布
var arr = [1,2,3,4,5];
function randomsort(a, b) {
return Math.random()>.5 ? -1 : 1; //通过随机产生0到1的数,然后判断是否大于0.5从而影响排序,产生随机性的效果。
}
arr.sort(randomsort);
console.log(arr);