数组中随机取出其中三个元素
方法一实现:
var Arr=["aaa","bbb","ccc","ddd","eee","fff"]
var arrNew=[];
for(var i=0;i<3;i++)
{
var _num = Math.floor(Math.random()*Arr.length)
var mm = Arr[_num];
Arr.splice(_num,1)
arrNew.push(mm)
}
console.log(arrNew)
方法二实现:
<script>
var Arr=["aaa","bbb","ccc","ddd","eee","fff"]
function random (arr) {
return arr
.sort((x, y) => {
return Math.random() > 0.5 ? -1 : 1;
})
.slice(0, 3)
}
console.log(random(Arr))
</script>