码迷,mamicode.com
首页 > 编程语言 > 详细

javaScript从数组里随机抽取10个值

时间:2018-01-26 12:41:58      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:ons   抽取   rip   random   gpo   shuffle   dex   --   return   

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);
}

var items = [‘1‘,‘2‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘10‘,"11","12","13","14","15"];

console.log( getRandomArrayElements(items, 10) );

 

javaScript从数组里随机抽取10个值

标签:ons   抽取   rip   random   gpo   shuffle   dex   --   return   

原文地址:https://www.cnblogs.com/kelly07/p/8358669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!