Created
November 11, 2020 21:22
-
-
Save DoctorDerek/c9132398033c8254aa869ceaba7345f8 to your computer and use it in GitHub Desktop.
Using Set to Filter an Array For Unique Strings in JavaScript ES6
This file contains hidden or 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
const strings = ["👌", "👌", "👌", "🐱👤", "🐱👤", "🐱🚀", "🐱🚀"] | |
const stringsSet = new Set(strings) | |
const uniqueStrings = [...stringsSet] | |
console.log(uniqueStrings) | |
// Output: Array(3) [ "👌", "🐱👤", "🐱🚀" ] | |
// One-liner using the method Array.from(): | |
console.log(Array.from(new Set(strings))) | |
// Output: Array(3) [ "👌", "🐱👤", "🐱🚀" ] | |
// Equivalent to using the spread operator: | |
console.log([...new Set(strings)]) | |
// Output: Array(3) [ "👌", "🐱👤", "🐱🚀" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment