标签:UNC random mat fun 索引 长度 undefined war func
let ary = [2, 6, 5, 5, 99, 65, 5, 2, 989, 23, 65, 6 ]
function getDiff(arr) {
let obj = {}
for (let i = 0; i < arr.length; i++) {
let temp = arr[i]
if (obj[temp] === temp && temp !== undefined) {
arr.splice(i, 1)
i-- //数组长度变短 索引回到前一位
continue //跳出本次循环
}
obj[temp] = temp
}
obj = null
return arr
}
const resArr = getDiff(ary)
console.log(resArr) //[2, 6, 5, 99, 65, 989, 23]
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
function getNums(arr, count) {
let newArr = []
for (let i = 0; i < count; i++) {
let num = Math.floor(Math.random() * arr.length)
newArr.push(arr[num])
//arr.splice(num, 1)
arr[num] = arr[arr.length - 1]
arr.length = arr.length - 1
}
return newArr
}
const res = getNums(arr, 5)
console.log(res)
标签:UNC random mat fun 索引 长度 undefined war func
原文地址:https://www.cnblogs.com/sheep2/p/12715856.html