Created
December 18, 2020 00:29
-
-
Save DoctorDerek/36210807db004c3ba464f823737fe7d0 to your computer and use it in GitHub Desktop.
Get Unique Values From a JavaScript Array Using Set (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 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