js数组去重的几种方法

文章资讯 2020-07-19 14:30:47

js数组去重的几种方法

// 第一种:
function remove(arr){
return [...new Set(arr)]
}
console.log(remove([1,2,3,4,5,5,5,5]));


// 第二种 O(2n)
function removeColone(arr){
let final = [];
for(let i = 0 ;iif(arr[i] !==arr[i+1]){
final.push(arr[i]);
}
}
console.log(final)
}
removeColone([1,2,2,3,3,4,4,5,5,5,5,7,8,9,9])





// 第三种
function rmoveProtype(arr){
let tem ={};
for(let i = 0;item[${arr[i]}] = 'a'
}
let result=Object.keys(tem).map(res=>{
return parseInt(res)
})
return result;
}





//第四种includes() == -1
function removeRepeat(arr){
var tem = [];
for(let i = 0;iif(!arr.includes(arr[i])){
tem.push(arr[i])
}
}
console.log(tem)
}




//第五种filter
function removeRepeat(arr){
return arr.filter((currentValue,index,arr)=>{
return arr.indexOf(currentValue) ===index
})
}
console.log(removeRepeat([1,2,2,3,3,4,5,5,5,5]))