Created
May 23, 2023 00:52
-
-
Save kironroy/5fb04ad6aa5fc45ad4a85397147ee566 to your computer and use it in GitHub Desktop.
Sets in js
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
// sets have no order and only store unique items. | |
// no need to get data out of a set | |
const ordersSet = new Set(['Pasta', 'Pizza', 'Risotto', 'Pasta', 'Pizza']); | |
console.log(ordersSet); | |
console.log(new Set('Vini')); | |
console.log(ordersSet.size); | |
console.log(ordersSet.has('Bread')); | |
ordersSet.add('Garlic Bread'); | |
ordersSet.add('Garlic Bread'); | |
ordersSet.delete('Risotto'); | |
// ordersSet.clear(); | |
console.log(ordersSet); | |
for (const order of ordersSet) console.log(order); | |
// Example | |
// Working with arrays and sets | |
const staff = ['Waiter', 'Chef', 'Waiter', 'Manager', 'Chef', 'Waiter']; | |
const staffUniqueAsASet = new Set(staff); | |
console.log(staffUniqueAsASet); | |
// conversion from set to array | |
const staffUniqueAsAnArr = [...new Set(staff)]; | |
console.log(staffUniqueAsAnArr); | |
console.log(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment