首页 > 代码库 > [转]JavaScript去重的6种方法
[转]JavaScript去重的6种方法
Array.prototype.unique1 = function(){ var n = []; for(var i = 0; i < this.length; i++) { if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n;}Array.prototype.unique2 = function(){ var n = {},r=[]; for(var i = 0; i < this.length; i++) { if (!n[this[i]]) { n[this[i]] = true; r.push(this[i]); } } return r;}Array.prototype.unique3 = function(){ var n = [this[0]]; for(var i = 1; i < this.length; i++) { if (this.indexOf(this[i]) == i) n.push(this[i]); } return n;}Array.prototype.unique4 = function(){ this.sort(); var re=[this[0]]; for(var i = 1; i < this.length; i++) { if( this[i] !== re[re.length-1]) { re.push(this[i]); } } return re;} Array.prototype.unique5 = function(){ var self = this; var _a = this.concat().sort(); _a.sort(function(a,b){ if(a == b){ var n = self.indexOf(a); self.splice(n,1); } }); return self;};Array.prototype.unique6 = function(){ return this.reduce(function(p, c) { if (p.indexOf(c) < 0) p.push(c); return p; }, []); };
Performance test: ( running with an array of 10000 random numbers )
method 1 used 349ms
method 2 used 5ms
method 3 used 442ms
method 4 used 15ms
method 5 used 13ms
method 6 used 424ms
So, the best idea to unique an array is to use a "hash map" to check if an item is repeated.
There is a demo page: http://php.js.cn/down/sample/array.html
[转]JavaScript去重的6种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。