Skip to content

Instantly share code, notes, and snippets.

@wujunchuan
Last active May 10, 2019 09:18
Show Gist options
  • Save wujunchuan/b399ae383863fd2c0d21f69ee4413ee1 to your computer and use it in GitHub Desktop.
Save wujunchuan/b399ae383863fd2c0d21f69ee4413ee1 to your computer and use it in GitHub Desktop.
数组去重 Array dedupe #js #util
// 利用ES6的Set数据结构来数组去重
// 方法1
let array = [1,2,2,2,1,2,2,3,4];
function dedupe(array) {
return Array.from(new Set(array));
}
var a = dedupe(array);
console.log(a);//[ 1, 2, 3, 4 ]
//方法二 利用语法糖 ...
// 其实就是内部的一个for..of 循环
console.log([...new Set(array)]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment