【Set去重】
const weightRemoval1 = (ary)=>{
return [...new Set(ary)]
}
【indexOf去重】
const weightRemoval2 = (ary)=>{
let ary2 = []
ary.forEach(item=>{
ary2.indexOf(item) == -1 && ary2.push(item)
})
return ary2
}
【includes去重】
const weightRemoval2 = (ary)=>{
let ary2 = []
ary.forEach(item=>{
!ary2.includes(item) == -1 && ary2.push(item)
})
return ary2
}
【递归去重】
const weightRemoval4 = (ary,index = 0)=>{
if(index > ary.length - 1)return
let firstIndex = ary.indexOf(ary[index])
let lastIndex = ary.lastIndexOf(ary[index])
if(firstIndex != -1 && (firstIndex != lastIndex)){
ary.splice(firstIndex,1)
weightRemoval4(ary,index)
}else{
weightRemoval4(ary,index + 1)
}
}