1,常规双循环去重(缺点:循环次数较多)
Array.prototype.unique1 = function(){注意:
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0;
let res = [that[0]];
for(let i = 1; i < len; i++){
let falg = false;
for(let j = 0; j < res.length; j++){
if(that[i] === res[j]){
falg = true;
break;
}
}
if(!falg){
res.push(this[i]);
}
}
return res;
}
(1,必须在第二个循环外push到新的数组
(2,减少循环次数,在第二个循环中找到相等值,马上退出该循环
(3,每次循环对falg检验
(4,由于第一值直接赋值,所以不用检测第一个值
2,数组的sort先排序再去重(缺点:返回数组为排序后的顺序)
Array.prototype.unique2 = function(){3,对象键值不重复(缺点:占内存)
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this).sort(),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(that[i] !== res[res.length - 1]){res.push(that[i]);}
}
return res;
}
Array.prototype.unique3 = function(){
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,obj = {},res = [];
for(let i = 0; i < len; i++){
let type = typeof that[i];
if(!obj[that[i]]){
res.push(that[i]);
obj[that[i]] = [type];
}else if(obj[that[i]].indexOf(type) === -1){
res.push(that[i]);
obj[that[i]].push(type);
}
}
return res;
}
4,indexOf检测新数组(优点:简单)
Array.prototype.unique4 = function(){5,indexOf检测原数组
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(res.indexOf(that[i]) === -1){res.push(that[i]);}
}
return res;
}
Array.prototype.unique5 = function(){
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(that.indexOf(that[i]) === i){res.push(that[i]);}
}
return res;
}
6,数组的every方法
Array.prototype.unique6 = function(){注意:如果发现了一个这样的元素,every 方法将会立即返回 false。否则,callback 为每一个元素返回 true,every 就会返回 true。
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(res.every(function(val){return val !== that[i]})){
res.push(that[i]);
}
}
return res;
}
7,数组的some方法
Array.prototype.unique10 = function(){注意:如果找到了这样一个值,some 将会立即返回 true。否则,some 返回 false。
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(!res.some(function(val){return val === that[i]})){
res.push(that[i]);
}
}
return res;
}
8,数组的includes方法
Array.prototype.unique7 = function(){9,数组的filter方法
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(!res.includes(that[i])){
res.push(that[i]);
}
}
return res;
}
Array.prototype.unique8 = function(){10,数组的find方法(缺点:没有做0或者false这个一类判断)
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(res.filter(function(val){return val === that[i]}).length === 0){
res.push(that[i]);
}
}
return res;
}
Array.prototype.unique9 = function(){还有lastIndexOf,findIndex等方法也能做去重,就不一一列举,有兴趣的可以自己做一下。
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length >>> 0,res = [that[0]];
for(let i = 1; i < len; i++){
if(!res.find(function(val){return val === that[i]})){
res.push(that[i]);
}
}
return res;
}