依照对象的属性名唯一性的思路
var ary=[1,2,3,3,3,3,5,4,1,2,3,5,4,1,3,5,2,15]
var obj={};
for(var i=0;i<ary.length;i++){
var cur=ary[i];
if(obj[cur]==cur){
ary.splice(i,1);
i--;
continue;
}
obj[cur]=cur;
}
console.log(ary)
var ary=[1,2,3,3,3,3,5,4,1,2,3,5,4,1,3,5,2,15]
var obj={};
for(var i=0;i<ary.length;i++){
var cur=ary[i];
if(obj[cur]==cur){
ary[i]=ary[ary.length-1];
ary.length--;
i--;
continue;
}
obj[cur]=cur;
}
console.log(ary)
第二种可以提高性能,但去重后的数组各项的位置可能会有调换
网友评论