从数组items中随机取出一个元素
var item = items[Math.floor(Math.random()*items.length)];
例子:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
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 = ['景临境', '何开', '吴者然', '赵柔', '陆梦', '暴天明', '左阻', '落月清', '墨学之'];
console.log(getRandomArrayElements(items, 4));
</script>
</body>
</html>
效果图:
标签:index,arr,shuffled,min,items,JS,随机,数组,Math From: https://blog.51cto.com/u_16171388/6558659