js去除数组重复项,删除重复数据的方法

var arr = [3,3,1,6,7,4,4,1];   
var o = {};   
for(var i = 0; i < arr.length; i++)   
{   
  o[arr[i]] = arr[i];   
}   
arr = [];   
for (var el in o)   
{   
  if (el == o[el])   
  arr.push(el - 0);   
}   
alert(arr); 


 

var clearRepeat=function(a){   
            var c=[],b={};   
               
            for(var i=0;i<a.length;i++){   
                    if( ! b[0+a[i]]){   
                            b[a[i]]=1;   
                            c.push(a[i])   
                        }   
                }   
            return c;   
        }   
       
    var a=[undefined,4,5,6,7,8,9,10,11,"aa","bb","aa","bb","$#@@","!*&^%%","$#@@","!*&^%%",0,"0"];   
    //请注意上面数组的0,"0"项目。是可以正常处理的。因为他们的数据类型不一样所以他不能算是重复项   
       
    var c=clearRepeat(a);   
    alert(c.join())


 

Array.prototype.del = function() { 
var a = {}, c = [], l = this.length; 
for (var i = 0; i < l; i++) { 
  var b = this[i]; 
  var d = (typeof b) + b; 
  if (a[d] === undefined) { 
    c.push(b); 
    a[d] = 1; 
  } 
} 
return c; 
} 
alert([1, 1, 2, 3, 4, 5, 4, 3, 4, 4, 5, 5, 6, 7].del()); 

 

Array.prototype.unique = function(){
    var o = {}, re = [];
    for(var i=0, len = this.length;i<len; i++){
        if(!o[this[i]]){
            o[this[i]] = true;
            re.push(this[i]);
        }
    }
    return re;
}
var arr = [1,2,3,2,3,4,1,2,3,1,1,1,5,6];

console.log( arr.unique() );


 

var arr = ["123","123","123","123","sfsdf","123","345","123","123","345","456","567","sdc"]; 
var str = []; 
for(var i = 0,len = arr.length;i < len;i++){ 
  !RegExp(arr[i],"g").test(str.join(",")) && (str.push(arr[i])); 
} 
alert(str); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值