Created
November 25, 2020 02:35
-
-
Save whal-e3/e0e6911b4300ea23ae89f4799dfa99c8 to your computer and use it in GitHub Desktop.
JS sets (unique values)
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
// SET - unique values | |
const set1 = new Set(); | |
set1.add(100); | |
set1.add('A string'); | |
set1.add({ name: 'John' }); | |
set1.add(true); | |
set1.add(100); | |
const set2 = new Set([1, true, 'string', {}]); | |
console.log(set1.size); | |
// Check | |
console.log(set1.has(100)); | |
console.log(set1.has(10 + 90)); | |
console.log(set1.has({ name: 'John' })); | |
// Delete | |
set1.delete(100); | |
// Iterate | |
for (let item of set1) { | |
console.log(item); | |
} | |
set1.forEach(value => { | |
console.log(value); | |
}); | |
// Convert to Array | |
const setArr = Array.from(set1); | |
console.log(setArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment