js去除数组重复项,删除重复数据的方法
程序员文章站
2022-03-04 12:45:45
...
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())