var arr=[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN];
执行arr.uniq();
返回一个新的数组:[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']
值得注意的是,NaN与NaN不相等。
Array.prototype.uniq = function () {
var f=false;
for(var i=0;i<this.length;i++){
if(this[i] !== this[i]){
f=true;
}
for(var j=i+1;j<this.length;){
if( this[i] === this[j] || (f && this[j] !== this[j]) ){
this.splice(j,1);//arrayObject.splice(起始位置,长度,添加的元素)
}
else{
j++;
}
}
}
return this;
}