// 数组去重1:filter+indexOf
function uniqueArr(arr = []){
return arr.filter((item,index,array)=>{
return index === array.indexOf(item)
})
}
// 数组去重2:reduce+includes
function uniqueArr(arr = []){
return arr.reduce((pre,cur)=>{
return pre.includes(cur) ? pre: pre.concat(cur)
},[])
}
// 数组去重3:forEach+indexOf
function uniqueArr(arr = []){
const newArr = []
arr.forEach(item=>{
if(!~newArr.indexOf(item)){
newArr.push(item)
}
})
return newArr
}
// 数组去重4:new Set()
function uniqueArr(arr = []){
return [...new Set(arr)]
}
const arr = uniqueArr([1,2,3,2,1])
console.log(arr)
console