zoukankan      html  css  js  c++  java
  • Javascript作业—数组去重(要求:原型链上添加函数)

    数组去重(要求:原型链上添加函数)

    <script>
    //数组去重,要求:在原型链上添加函数
    
    //存储不重复的--仅循环一次
    if(!Array.prototype.unique1){
        Array.prototype.unique1=function(){
            var item ,
                hash={},
                len=this.length,
                result=[];
            for(var i = 0 ; i < len ; i++){
                item = this[i];
                if(!hash[item+Object.prototype.toString.call(item)]){ //1 和 '1'的处理
                    result.push(item);
                    hash[item+Object.prototype.toString.call(item)]=true;
                }
            }
            return result;
        }
    }
     
    //去除重复的--循环次数太多,不好
    Array.prototype.unique2=function (){
        for(var i=0;i <=this.length;i++){
            for(var j=i+1;j<=this.length;j++)
            {
                if(arr[i]===arr[j]){arr.splice(j,1);j--;} //去除重复
            }
        }
        return arr;
    }
    
    
    var arr=[1,'1',2,3,3,4,5,4,4];
    
    </script>
  • 相关阅读:
    十二道MR习题
    十二道MR习题 – 1 – 排序
    初识HBase
    Java内存分析1
    scala学习手记40
    scala学习手记40
    scala学习手记39
    scala学习手记38
    scala学习手记37
    scala学习手记36
  • 原文地址:https://www.cnblogs.com/dinghanhua/p/9708139.html
Copyright © 2011-2022 走看看