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)



 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
数组去重的set方法是一种使用ES6中的Set对象来实现的方法。Set对象是一种集合,它只能包含唯一的值,不允许重复。我们可以通过将数组转换为Set对象,然后再将Set对象转回数组的方式来实现数组去重。 具体步骤如下: 1. 创建一个新的Set对象,将原始数组作为参数传入。 2. 将Set对象转换为数组,得到去重后的数组。 以下是使用set方法实现数组去重的示例代码: ```javascript const arr = [1, 2, 3, 3, 4, 4, 5]; const uniqueArr = Array.from(new Set(arr)); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 在这个示例代码中,我们首先创建了一个新的Set对象,并将原始数组arr作为参数传入。然后,我们使用Array.from方法将Set对象转换为数组,得到去重后的数组uniqueArr。最后,我们打印出去重后的数组uniqueArr。 使用Set对象的方式是一种简单且高效的方法来实现数组去重。它利用了Set对象的特性,自动去除了重复的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [数组去重方法(Set对象)](https://blog.csdn.net/m0_56262444/article/details/116747048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JavaScript数组去重方法总结【12种方法,号称史上最全】](https://download.csdn.net/download/weixin_38691055/13476291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [高性能js数组去重(12种方法,史上最全)](https://download.csdn.net/download/weixin_38687928/14807219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rattenking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值