new Set()
let arr = [0,0,0,1,23,444,5,1,4,4,4]
let a = new Set(arr)
console.log(a)
利用new Map()去重
let arr = [0, 0, 0, 1, 23, 444, 5, 1, 4, 4, 4]
let a = new Map()
let b = []
arr.forEach(item => {
if (!a.has(item)) {
a.set(item,true)
b.push(item)
}
})
console.log(b)
includes 结合 forEach
let arr = [0, 0, 0, 1, 23, 444, 5, 1, 4, 4, 4]
let a = []
arr.forEach(item => {
if (!a.includes(item)) {
a.push(item)
}
})
console.log(a)
indexOf 结合 forEach
let arr = [0, 0, 0, 1, 23, 444, 5, 1, 4, 4, 4]
let a = []
arr.forEach(item => {
if (a.indexOf(item) === -1) {
a.push(item)
}
})
console.log(a)
利用对象的属性不能相同的特点进行去重
let arr = [0, 0, 0, 1, 23, 444, 5, 1, 4, 4, 4]
let a = {}
let b = []
arr.forEach(item => {
if (!a[item]) {
a[item] = 1
b.push(item)
}
})
console.log(b)