Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DoctorDerek/36210807db004c3ba464f823737fe7d0 to your computer and use it in GitHub Desktop.

Select an option

Save DoctorDerek/36210807db004c3ba464f823737fe7d0 to your computer and use it in GitHub Desktop.
Get Unique Values From a JavaScript Array Using Set (ES6)
const myArray = [2, 1, "🎊", "✨", true, false, false, true, "✨", "🎊", 1, 2]
const mySet = new Set(myArray)
const uniqueValues = Array.from(mySet)
console.log(uniqueValues)
// Output: Array(6) [2, 1, "🎊", "✨", true, false]
// Using the ... spread operator is the same as Array.from():
console.log([...mySet])
// Output: Array(6) [2, 1, "🎊", "✨", true, false]
const duplicateCount = myArray.length - uniqueValues.length
console.log(`${duplicateCount} duplicates removed`)
// Output: 7 duplicates removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment