从数组中随机取值 <!DOCTYPE html>window.onload=function() {vararr=["009","002","006","003","001","005"];varindex=Math.floor((Math.random()*arr.length)); console.log(arr[index]); } 914 151617181920
<!DOCTYPE html>window.onload = function() { let arr = ['009', '002', '006', '003', '001', '005'] let index = Math.floor((Math.random()*arr.length)) console.log(arr[index]) }
JS中数组随机排序实现(原地算法sortshuffle算法) compare(a,b)中,a、b都是比较参数,当a-b>0 ,交换位置a-b=0,位置不变a-b随机排序我们都会想到Math的random方法,具体实现如下,但是这样操作确有缺陷,理论很丰满...1、方法一(不推荐)arr.sort(() => Math.random() - 0.5)缺陷:chrome浏览器对于数组长度为...