Array 数组去重 总结10方法(7)

时间:2022-05-16 19:03:58

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(){
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;
}
3,对象键值不重复(缺点:占内存)

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(){
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;
}
5,indexOf检测原数组
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(){
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;
}
注意:如果发现了一个这样的元素,every 方法将会立即返回 false。否则,callback 为每一个元素返回 true,every 就会返回 true。

7,数组的some方法

Array.prototype.unique10 = 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(!res.some(function(val){return val === that[i]})){
res.push(that[i]);
}
}
return res;
}
注意:如果找到了这样一个值,some 将会立即返回 true。否则,some 返回 false。
8,数组的includes方法

Array.prototype.unique7 = 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(!res.includes(that[i])){
res.push(that[i]);
}
}
return res;
}
9,数组的filter方法

Array.prototype.unique8 = 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(res.filter(function(val){return val === that[i]}).length === 0){
res.push(that[i]);
}
}
return res;
}
10,数组的find方法(缺点:没有做0或者false这个一类判断)

Array.prototype.unique9 = 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(!res.find(function(val){return val === that[i]})){
res.push(that[i]);
}
}
return res;
}
还有lastIndexOf,findIndex等方法也能做去重,就不一一列举,有兴趣的可以自己做一下。