基于原型扩展方法
数值去重
function unque(ary){ let obj = {}; for(var i = 0;i<ary.length-1;i++){ let item = ary[i]; if(typeof obj[item] !== 'undefined'){ ary[i] = ary[ary.length-1]; ary.length--; i--; continue; } obj[item] = item } obj = null; return ary; } let ary = [12,123,12,13,5,2,123,123,5,12,16];
2.基于原型扩展
~function(){ function myUnque(){ let obj = {}; for(var i = 0; i<this.length-1;i++){ let item = this[i] if(typeof obj[item]!=="undefined"){ this[i] = this[this.length-1] this.length--; i--; continue; } obj[item] = item } obj = null; return this; } Array.prototype.myUnque = myUnque }() let ary = [12,123,12,13,5,2,123,123,5,12,16]; ary.myUnque();

浙公网安备 33010602011771号