Last active
May 10, 2019 09:18
-
-
Save wujunchuan/b399ae383863fd2c0d21f69ee4413ee1 to your computer and use it in GitHub Desktop.
数组去重 Array dedupe #js #util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 利用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