// 去除数组的重复成员
[...new Set(array)]
方法二:
function dedupe(array) {
  return Array.from(new Set(array));
}

dedupe([1, 1, 2, 3]) // [1, 2, 3]
原理------------:ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。
const s = new Set();
[2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));
for (let i of s) {
  console.log(i);
}
// 2 3 5 4


上面的方法也可以用于,去除字符串里面的重复字符。
[...new Set('ababbc')].join('')
// "abc"

 

相关文章:

  • 2021-05-17
  • 2021-12-04
  • 2022-12-23
  • 2021-11-01
  • 2021-12-25
  • 2021-05-20
  • 2021-05-28
  • 2022-12-23
猜你喜欢
  • 2022-01-16
  • 2021-07-09
  • 2022-12-23
  • 2022-01-27
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案