Array 去重

[javascript] Array.prototype.distinct = function(){ var i = 0; while( i < this.length ){ var n = this[i]; this.splice(i,1,null); if(this.indexOf(n) < 0){ this.splice(i,1,n); i++; }else{ this.splice(i,1); } } return this; }; var t= [1,2,3,3,4,3,4,4,5,6]; Array.prototype.distinct= function(){ var self = this; this.slice().sort().sort(function(a,b){ if(a==b){ var n = self.indexOf(a)&& self.splice( n ,1); } }) return this; } Array.prototype.distinct= function() { var temp = {},n=[]; for(var i=0,len=this.length;i < len; i++) { if(typeof temp[this[i]] == "undefined") { temp[this[i]] = 1; n.push(this[i]) } } return n; }; console.log( t.distinct() ); [/javascript]
posted @ 2011-02-19 18:38  7hihi  阅读(120)  评论(0编辑  收藏  举报