数组去重的3个实例:
1、
var arrs = [1, 2, "3", 4, 5, 2, 5, 2, "1", 3, 3];
var arr = new Set(arrs);
2、
var arrs = [1, 2, "3", 4, 5, 2, 5, 2, "1", 3, 3];
var obj = {};
var arr = [];
arrs.forEach(function(a){
let key = typeof a + a;
if (!obj[key]) {
arr.push(a);
obj[key] = true;
}
})
console.log(arr);
3、
var arrs = [1, 2, "3", 4, 5, 2, 5, 2, "1", 3, 3];
var arr = [];
for(let i = 0; i < arrs.length; i++) {
if(arr.indexOf(arrs[i]) == -1) {
arr.push(arrs[i]);
}
}
console.log(arr)
4、使用filter
const arr = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
let arr2 = arr.filter((item, index, obj) => {
return obj.indexOf(item) === index
})
console.log(arr) // [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]
console.log(arr2) // [1, 2, 3, 5, 4]