js数组去重

时间:2019-02-22 12:11:17   收藏:0   阅读:173

数组去重:

Array.prototype.unique = function(){
//利用了对象属性名的唯一性
	var obj = {},
		len = this.length,
		newArr = [];
	for(var i =0;i<len;i++)
	{
		if(!obj[this[i]]){
			obj[this[i]] = i+1;
			newArr.push(this[i]);
		}
	}
	return newArr;
}

对象数组去重:

Array.prototype.unique = function(key){
//利用了对象属性名的唯一性
	var obj = {},
		len = this.length,
		newArr = [];
	for(var i =0;i<len;i++)
	{
		if(!obj[this[i][key]]){
			newArr.push(this[i]);
			obj[arr[i][key]] = true;
		}
	}
	return newArr;
}

  

 

原文:https://www.cnblogs.com/coce/p/10417552.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!