方法:
Array.createRandomNum = function (num,max) { var result = []; try { while (num--) { result.push(Math.ceil(Math.random() * max)); } } catch (e) { console.log(e); } return result; } Array.prototype.distinct = function () { var cur = [], result = [], obj = {}; try { cur = this; cur.forEach(t => obj[t] = null); result = Object.keys(obj); } catch (e) { console.log(e); } return result; }
调用:
var arr = Array.createRandomNum(100000, 10000); console.log(arr); var arr1 = arr.distinct(); console.log(arr1); console.timeEnd("计时器结束");