码迷,mamicode.com
首页 > 编程语言 > 详细

数组去重复

时间:2017-06-02 11:25:01      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:res   this   ons   doctype   class   tle   bsp   var   meta   

举例 

数组  [1,1,7,4] 去重,并且去掉重复的选项为 [7,4]

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
</head>
<body>
	<script>
		Array.prototype.unique3 = function(){
		 var res   =  [];
		 var json  =  {};
		 var count =  0 ;
		 var obj   =  {};
		 for(var i = 0; i < this.length; i++){
		    if(!json[this[i]]){  //未存
		       res.push(this[i]);
		       json[this[i]] = ++count;
		    }else{               //已存
		    	if(!obj[this[i]]){ //首次
		    		obj[this[i]] = 1;
		    		for(var j=0;j<res.length;j++){
		    			if(res[j]==this[i]){
		    				res.splice(j,1)
		    			}
		    		}		    		
		    	}
		    }
		 }
		 return res;
		}
		var arr = [1,1,7,4,7];
		console.log(arr.unique3());	
	</script>
</body>
</html>

  

数组去重复

标签:res   this   ons   doctype   class   tle   bsp   var   meta   

原文地址:http://www.cnblogs.com/sunjingxin/p/6932094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!