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


    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等方法也能做去重,就不一一列举,有兴趣的可以自己做一下。

    其他

    [我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)

    [我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_38082783)

    [微信小程序专栏](https://blog.csdn.net/column/details/18335.html)

    [前端笔记专栏](https://blog.csdn.net/column/details/18321.html)

    [微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_38082783/10244082)

    [微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_38082783/10196944)

    [微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)

    [微信小程序实例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)

    [前端笔记列表](http://blog.csdn.net/m0_38082783/article/details/79208205)

    [游戏列表](http://blog.csdn.net/m0_38082783/article/details/79035621)



     

  • 相关阅读:
    小米笔试(09-15)
    58同城笔试(09-14)
    Java中的static总结
    【Linux】常用代码
    【项目部署】上线
    入职一年高校的体会
    小团队管理总结
    从windows到Linux的再次转身,这次是硬件开发
    了解到雪花算法
    CCAI观后
  • 原文地址:https://www.cnblogs.com/linewman/p/9918546.html
Copyright © 2020-2023  润新知