//从数组中获取随机的N个数|//从数组中获取随机的N个数 且不重复

【//从数组中获取随机的N个数|//从数组中获取随机的N个数 且不重复】//从数组中获取随机的N个数 且不重复
var arr1 = [‘张三’, ‘李四’, ‘王五’, ‘嘿嘿’, ‘xixi’, ‘sb’, ‘nide’]
function randPer(arr, num) {
var newarr = []
for (let i = 0; i < num; i++) {
let rand = Math.floor(Math.random() * arr.length)
newarr.push(arr[rand])
arr = removeWithoutCopy(arr, arr[rand])
}
return newarr
}
//移除数组中的指定元素
function removeWithoutCopy(arr, item) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
arr.splice(i, 1)
i–
}
}
return arr
}
console.log(arr1)

    推荐阅读