数组去重的几种方法

整理了几种数组去重比较简单的方案

1.使用new set()
const arr = [1,2,5,4,2,4,5,7,6,5,4];
const newArr = [...new set(arr)]
console.log(newArr ); // [1,2,5,4,7,6]
2.使用includes()方法;
const arr = [1,2,5,4,2,4,5,7,6,5,4];
const newArr = [];
arr.forEach((ele, i) => {
  if (!newArr .includes(ele)){
    newArr.push(ele)
  }
})
console.log(newArr ); // [1,2,5,4,7,6]

你可能感兴趣的:(数组去重的几种方法)