Skip to content

Instantly share code, notes, and snippets.

@kironroy
Created May 23, 2023 00:52
Show Gist options
  • Save kironroy/5fb04ad6aa5fc45ad4a85397147ee566 to your computer and use it in GitHub Desktop.
Save kironroy/5fb04ad6aa5fc45ad4a85397147ee566 to your computer and use it in GitHub Desktop.
Sets in js
// 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