1. Array.form(new set())去重
let arr_1 = [88,66,11,66,88,11,22,88,11]
let arr_result_1 = Array.form(new set(arr_1)) //这里就好像python中的set函数,直接调用set()函数去重
console.log(arr_result_1)
// [88,66,11,22]
2. includes去重
let arr_2 = [88,66,11,66,88,11,22,88,11]
let arr_end_2 = []
arr.forEach(item => {
// includes 中文“包括”
if(!arr_end_2.includes(item)) { //判断arr_end中是否含有item,false代表没有,则push(item)
arr_end_2.push(item)
}
})
console.log(arr_end_2)
// arr_end: [88,66,11,22]
3. 双重for循环去重(不改变原数组)
let arr_3 = [88,66,11,66,88,11,22,88,11]
let arr_3_copy = arr_3
for(let x=0;x<=arr_3_copy .length;x++) {
for(let y=0;y<=arr_3_copy .length;y++){
if(arr_3_copy [x] == arr_3_copy [y] && x != y){
// 这里需注意的是 x != y 不加该条件约束,结果永远是 []空数组
// 是为了不让其移除第一个值,第一个相同的值肯定是不重复的,然后借助不同值的第一个值去判断,如果再有相同的值就移除
arr_3_copy .splice(y,1)
}
}
}
console.log(arr_3_copy )
// arr_3_copy : [88,66,11,22]
// arr_3 : [88,66,11,66,88,11,22,88,11] 原数组
4. 双重for循环去重(改变原数组)
let arr_4 = [88,66,11,66,88,11,22,88,11]
for(let x=0;x<=arr_4.length;x++) {
for(let y=0;y<=arr_4.length;y++){
if(arr_4[x] == arr_4[y] && x != y){ // 这里需注意的是 x != y 不加该条件约束,结果永远是 []空数组
arr_4.splice(y,1)
}
}
}
console.log(arr_4)
// arr_4 : [88,66,11,22]
5. indexOf 去重
let arr_5 = [88,66,11,66,88,11,22,88,11]
let arr_end_5 = []
for(let i=0;i<=arr_5.length;i++) {
if(arr_end_5.indexOf(arr_5[i]) == -1){ //indexOf() 判断字符是否存在,不存在,返回 -1
arr_end_5.push(arr_5[i])
}
}
// arr_end_5 : [88,66,11,22]
除了以上的方法去重外,还有很多,这里小编知道还有一个filter过滤去重
,so sorry , but该方法小编还没有完全搞懂它的去重原理。这里小编就不写了,待小编搞懂、搞明白再加上