欢迎来到天天文库
浏览记录
ID:40557718
大小:32.00 KB
页数:4页
时间:2019-08-04
《js去重复几个例子》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JS数组去重复的几个案例,给大家分享一下。最后附一个HTML代码示例。第一种:也是最笨的吧。Array.prototype.unique1=function(){varr=newArray();label:for(vari=0,n=this.length;i2、urnthis.sort().join(",,").replace(/(,3、^)([^,]+)(,,2)+(,4、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");}第三种:使用对象的【hasOwnProperty】方法Array.prototype.unique3=function(){vartemp={},len=this.length;for(vari=0;i5、temp[this[i]]="mygod";}}len=0;vartempArr=[];for(variintemp){tempArr[len++]=i;}returntempArr;}第四种:先排序,前项比后项。Array.prototype.unique4=function(){vartemp=newArray();this.sort();for(i=0;i6、经常用的,效率也很好。有点想hash表的感觉。viewsourceprint?Array.prototype.unique5=function(){varres=[],hash={};for(vari=0,elem;(elem=this[i])!=null;i++){if(!hash[elem]){res.push(elem);hash[elem]=true;}}returnres;}下面代码是例子,可以复制到本地使用。7、AD>NewDocumentvararr=["123","123","123","123","sfsdf","123","345","123","123","345","456","58、67","sdc"];//方法一:正则functionuniqueA(){alert(arr.sort().join(",,").replace(/(,9、^)([^,]+)(,,2)+(,10、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(","));}//方法二:排序后比较functionuniqueB(){varnewArr=newArray();arr=arr.sort();varc=arr.length;for(i=0;i11、==arr[i+1]){continue;}newArr[newArr.length]=arr[i];}alert(newArr);}//方法三:functionuniqueC(){vartemp={},len=arr.length;for(vari=0;i12、SCRIPT>
2、urnthis.sort().join(",,").replace(/(,
3、^)([^,]+)(,,2)+(,
4、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");}第三种:使用对象的【hasOwnProperty】方法Array.prototype.unique3=function(){vartemp={},len=this.length;for(vari=0;i5、temp[this[i]]="mygod";}}len=0;vartempArr=[];for(variintemp){tempArr[len++]=i;}returntempArr;}第四种:先排序,前项比后项。Array.prototype.unique4=function(){vartemp=newArray();this.sort();for(i=0;i6、经常用的,效率也很好。有点想hash表的感觉。viewsourceprint?Array.prototype.unique5=function(){varres=[],hash={};for(vari=0,elem;(elem=this[i])!=null;i++){if(!hash[elem]){res.push(elem);hash[elem]=true;}}returnres;}下面代码是例子,可以复制到本地使用。7、AD>NewDocumentvararr=["123","123","123","123","sfsdf","123","345","123","123","345","456","58、67","sdc"];//方法一:正则functionuniqueA(){alert(arr.sort().join(",,").replace(/(,9、^)([^,]+)(,,2)+(,10、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(","));}//方法二:排序后比较functionuniqueB(){varnewArr=newArray();arr=arr.sort();varc=arr.length;for(i=0;i11、==arr[i+1]){continue;}newArr[newArr.length]=arr[i];}alert(newArr);}//方法三:functionuniqueC(){vartemp={},len=arr.length;for(vari=0;i12、SCRIPT>
5、temp[this[i]]="mygod";}}len=0;vartempArr=[];for(variintemp){tempArr[len++]=i;}returntempArr;}第四种:先排序,前项比后项。Array.prototype.unique4=function(){vartemp=newArray();this.sort();for(i=0;i6、经常用的,效率也很好。有点想hash表的感觉。viewsourceprint?Array.prototype.unique5=function(){varres=[],hash={};for(vari=0,elem;(elem=this[i])!=null;i++){if(!hash[elem]){res.push(elem);hash[elem]=true;}}returnres;}下面代码是例子,可以复制到本地使用。7、AD>NewDocumentvararr=["123","123","123","123","sfsdf","123","345","123","123","345","456","58、67","sdc"];//方法一:正则functionuniqueA(){alert(arr.sort().join(",,").replace(/(,9、^)([^,]+)(,,2)+(,10、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(","));}//方法二:排序后比较functionuniqueB(){varnewArr=newArray();arr=arr.sort();varc=arr.length;for(i=0;i11、==arr[i+1]){continue;}newArr[newArr.length]=arr[i];}alert(newArr);}//方法三:functionuniqueC(){vartemp={},len=arr.length;for(vari=0;i12、SCRIPT>
6、经常用的,效率也很好。有点想hash表的感觉。viewsourceprint?Array.prototype.unique5=function(){varres=[],hash={};for(vari=0,elem;(elem=this[i])!=null;i++){if(!hash[elem]){res.push(elem);hash[elem]=true;}}returnres;}下面代码是例子,可以复制到本地使用。7、AD>NewDocumentvararr=["123","123","123","123","sfsdf","123","345","123","123","345","456","5
7、AD>
8、67","sdc"];//方法一:正则functionuniqueA(){alert(arr.sort().join(",,").replace(/(,
9、^)([^,]+)(,,2)+(,
10、$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(","));}//方法二:排序后比较functionuniqueB(){varnewArr=newArray();arr=arr.sort();varc=arr.length;for(i=0;i11、==arr[i+1]){continue;}newArr[newArr.length]=arr[i];}alert(newArr);}//方法三:functionuniqueC(){vartemp={},len=arr.length;for(vari=0;i12、SCRIPT>
11、==arr[i+1]){continue;}newArr[newArr.length]=arr[i];}alert(newArr);}//方法三:functionuniqueC(){vartemp={},len=arr.length;for(vari=0;i12、SCRIPT>
12、SCRIPT>
此文档下载收益归作者所有