1.年会抽红包,100个人,每人1-10块,最后全体平均每人7块钱 (无小数)
demo(len) {
// 每人给1块
let arr = new Array(len).fill(1)
let money = 700 - len
// 随机找人给1块
while (money > 0) {
let i = parseInt(Math.random() * 100)
if (arr[i] < 10) {
arr[i]++
money--
}
}
return arr
},
2.获取不重复的随机数
getNotRepeatArr(len,num){ let arr = new Array() for(let i = 0;i<len;i++){ arr[i] = i } arr.sort(()=>0.5-Math.random()) return arr.slice(0,num) }
3.数组对象中根据指定key来分类
let arr = [ {a:1,b:2,c:3,value:'???'}, {a:1,b:2,c:3,value:'2'}, {a:1,b:3,c:3,value:'3'}, {a:1,b:3,c:3,value:'f'}, {a:2,b:2,c:3,value:'ds'}, {a:3,b:2,c:4,value:'3'}, {a:2,b:2,c:3,value:'v'}, {a:4,b:2,c:3,value:'v'}, {a:5,b:2,c:3,value:'v'}, {a:3,b:6,c:3,value:'v'}, ] // 返回大树组 根据a b c相同放在同个数组 demo(arr){ let res = [] let i = 0 let obj = {} arr.forEach(item=>{ const {a,b,c} = item let key = `${a}-${b}-${c}` console.log(key,obj[key],i) if(Number.isInteger(obj[key])){ res[obj[key]].push(item) }else { obj[key] = i res[i] = [item] i = i+1 } }) return res.filter(item=>item.length>1) }