1.Object.keys方法
Array.prototype.unique = function() { var obj = {},len = this.length; for(var i=0;i<len;i++){ obj[this[i]] = 1; } return Object.keys(obj); }
2.filter和indexOf方法
Array.prototype.unique = function(){ return this.filter(function(item,index,array){ return array.indexOf(item) >= index; }); }
3.sort和splice方法
Array.prototype.unique = function(){ this.sort(); for(var i=0;len=ths.length,i<len;i++){ if(this[i] === this[i+1]){ this.splice(i,1); i--; } } }
參考:https://segmentfault.com/a/1190000004605229
http://www.toutiao.com/i6356017394137170434/?tt_from=mobile_qq&utm_campaign=client_share&app=news_article&utm_source=mobile_qq&iid=6258282437&utm_medium=toutiao_android