Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoctorDerek/c9132398033c8254aa869ceaba7345f8 to your computer and use it in GitHub Desktop.
Save DoctorDerek/c9132398033c8254aa869ceaba7345f8 to your computer and use it in GitHub Desktop.
Using Set to Filter an Array For Unique Strings in JavaScript ES6
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