日期:2014-05-16 浏览次数:20370 次
<SCRIPT LANGUAGE="JavaScript"> Array.prototype.deleteEle=function(){ var arr=this,o={},newArr=[],i,n; for( i=0;i<arr.length;i++){ n=arr[i]+typeof(arr[i]);//如果不需要类型判断,直接将后面的去掉即可 if(typeof(o[n])==="undefined"){ newArr[newArr.length]=arr[i] o[n]=1;//缓存 } } return newArr; } var x= [1,2,3,4,5,2,3,4,6,7,8]; document.write('原始数组:'+x); document.write("<br />"); document.write('去重复后:'+x.deleteEle()); Array.prototype.distinct=function(){ var a=[],b=[]; for(var prop in this){ var d = this[prop]; if (d===a[prop]) continue; //防止循环到prototype if (b[d]!=1){ a.push(d); b[d]=1; } } return a; } var x=['a','b','c','d','b','a','e','a','b','c','d','b','a','e']; document.write('原始数组:'+x); document.write("<br />"); document.write('去重复后:'+x.distinct()); </script>