Skip to content

Instantly share code, notes, and snippets.

@whal-e3
Created November 25, 2020 02:35
Show Gist options
  • Save whal-e3/e0e6911b4300ea23ae89f4799dfa99c8 to your computer and use it in GitHub Desktop.
Save whal-e3/e0e6911b4300ea23ae89f4799dfa99c8 to your computer and use it in GitHub Desktop.
JS sets (unique values)
// 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