Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Created August 25, 2023 16:39
Show Gist options
  • Save karlhorky/c6f549cb295e51667a23ef4319a6c7a4 to your computer and use it in GitHub Desktop.
Save karlhorky/c6f549cb295e51667a23ef4319a6c7a4 to your computer and use it in GitHub Desktop.
Set Methods demo
const hosts = new Set(["Wes", "Scott", "Snickers"]);
const team = new Set(["Wes", "Scott", "Kaitlin", "Ben"]);
const fans = new Set(["Paige", "Nick", 1]);
// Difference between two sets
console.log(team.difference(hosts)); // Set { 'Kaitlin', 'Ben' }
// Overlap between two sets
console.log(team.intersection(hosts)); // Set { 'Wes', 'Scott' }
// Items that are in one set or the other, but not both
console.log(team.symmetricDifference(hosts)); // Set { 'Kaitlin', 'Ben', 'Snickers' }
// All items in both sets
console.log(team.union(hosts)); // Set { 'Wes', 'Scott', 'Scott', 'Kaitlin', 'Ben', 'Snickers' }
// Are Wes and Scott in this set?
console.log(new Set(["Wes", "Scott"]).isSubsetOf(hosts)); // true
// Does this set have all items in the passed in set?
console.log(hosts.isSupersetOf(new Set(["Wes", "Scott"]))); // true
// Is there no overlap between these two sets?
console.log(team.isDisjointFrom(hosts)); // false
console.log(team.isDisjointFrom(fans)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment