js数组去掉重复的元素

<script>
    Array.prototype.uniq 
= function() {  
        
var temp = {}, len = this.length;

        
for(var i=0; i < len; i++)  {  
            
if(typeof temp[this[i]] == "undefined") {
                temp[
this[i]] = 1;
            }  
        }  
        
this.length = 0;
        len 
= 0;
        
for(var i in temp) {  
            
this[len++= i;
        }  
        
return this;  
    }  
   
    
var a = ["sss","aaa","sss",4,2,3,1,1,"sss","aaa","sss",4,2,3,1,1
        ,
"sss","aaa","sss",4,2,3,1,1,"sss","aaa","sss",4,2,3,1,1
        ,
"sss","aaa","sss",4,2,3,1,1,"sss","aaa","sss",4,2,3,1,1
        ,
"sss","aaa","sss",4,2,3,1,1,"sss","aaa","sss",4,2,3,1,1];  
    
var b = [2,3,5,7]; 
    
//--------------------------时间测试开始-------------------------
    start = new Date().getTime(); 
    
var c = a.concat(a).concat(b).concat(a).concat(b).concat(a)
         .concat(b).concat(a).concat(b).concat(a).concat(b)
         .concat(a).concat(b).concat(b).concat(a).concat(b).uniq(); 

    document.write(c 
+ " 长度 " + c.length); 
    end 
= new Date().getTime();
    
        document.write(
" 耗时:" + (end - start) + "ms")
    
//--------------------------时间测试结束--------------------------
        //IE结果:sss,aaa,4,2,3,1,5,7 长度 8 耗时:15ms
        //FF结果:sss,aaa,4,2,3,1,5,7 长度 8 耗时:2ms
</script>

阅读更多
文章标签: 测试 function c
个人分类: js及界面技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭