var items = [ {name:'小白',age:'23'}, {name:'小白2',age:'23'}, {name:'小白3',age:'23'}, {name:'小白4',age:'23'}, {name:'小白5',age:'23'}, {name:'小白6',age:'23'}, {name:'小白7',age:'23'}, {name:'小白8',age:'23'}, {name:'小白9',age:'23'}, {name:'小白10',age:'23'} ];
//抽取随机数组 function getRandomArrayElements(arr, count) { var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index; while (i-- > min) { index = Math.floor((i + 1) * Math.random()); temp = shuffled[index]; shuffled[index] = shuffled[i]; shuffled[i] = temp; } return shuffled.slice(min); } //打印 console.log( getRandomArrayElements(items, 4) );
标签: 随机读取
上面是“js从题库中随机抽取指定数的题目”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_1865.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!