前端面试之数组去重

  var a=[1,2,3,4,4];
    var b=[];
    var c={};
//    方法一  indexOf属性 针对不存在元素会返回 -1  否则会返回字符的指定位置 
//    for( i in a){
//        if(b.indexOf(a[i])==-1){
//            b.push(a[i])
//
//        }
//    }
//    方法二 创建空变量 通过判断变量是否存在 赋值给变量相应的boolean 值
    for(var i=0; i<a.length;i++){
        if(!c[a[i]]){
            c[a[i]]=true;
           b.push(a[i]);
        }
        
    }

    console.log(b);
原文地址:https://www.cnblogs.com/shouzi/p/6849820.html