let arr = [0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 2, 3, 3, 3, 9, 8, 9];
//数组去重法1
console.log(Array.from(new Set(arr)));
//数组去重法2
console.log(...new Set(arr));
//数组去重法3
let newarr = [];
for (var i = 0; i < arr.length; i++) {
if (newarr.indexOf(arr[i]) == -1) {
newarr.push(arr[i]);
};
};
console.log(newarr);
//数组去重法4
let newarr2 = [];
arr.sort(); //先将数组排序
// newarr2.push(arr[0]);
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i + 1]) { //判断后一项 是否跟前一项一样 将后一项放入新数组
newarr2.push(arr[i]);
};
};
console.log(newarr2);
//数组去重 法5
var res = [arr[0]];
for (var i = 0; i < arr.length; i++) {
var repeat = false;
// console.log(res);
for (var j = 0; j < res.length; j++) {
if (arr[i] == res[j]) {
repeat = true;
break;
}
}
if (!repeat) {
res.push(arr[i]);
}
}
console.log(res);
//数组去重 法6
var res = [];
var json = {};
for (var i = 0; i < arr.length; i++) {
if (!json[arr[i]]) {
res.push(arr[i]);
json[arr[i]] = 1;
}
}
console.log(res);
原文地址:https://www.cnblogs.com/shenbo666/p/9408369.html