//只适合生成随机不重复的纯num,var arr = [];function newRandomnum (num, firstRange, endRange){ var obj = {}; for(var i=0; i<num; i++){ var randomnum = parseInt(Math.random()*(endRange - firstRange)) + firstRange; if(!obj[randomnum]){ obj[randomnum] = true; arr.push(randomnum); }else{ i--; } } return arr;} newRandomnum(40000, 600, 100000);console.log(arr); //去重方法
function distinctArray(arr){ var obj={}; for(var i=0;i<arr.length;i++){ if(!obj[arr[i]]){ obj[arr[i]] =true; }else{ arr.splice(i, 1); } } return arr;}var testarr=[1,2,3,2];console.log(distinctArray(testarr));// [1,2,3]
时间: 2024-10-26 03:12:10