var arry =[1,25,15,1,2,15,5,15,25,35,1];
varset =newSet(arry);
console.log([...set]);
数组内置filter方法(兼容ie9+)
var arry =[1,25,15,1,2,15,5,15,25,35,1];
var newArry = arry.filter(function(currenValue,index,arr){
return index == arr.indexOf(currenValue);
})
console.log(newArry);
赋值给空数组加indexOf检测
var arry =[1,25,15,1,2,15,5,15,25,35,1];
var arr2 = [];
for(var i= 0; i<arry.length;i++){
if(arr2.indexOf(arry[i]) == -1) {
arr2.push(arry[i]);
}
}
console.log(arr2);
通过检测属性的方法
Array.prototype.unique3 = function(){var res = [];
var json = {};
for(var i = 0; i < this.length; i++){
if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
}
var arr = [112,112,34,'你好',112,112,34,'你好','str','str1'];
alert(arr.unique3());