Frontend Masters Boost RSS Feed https://frontendmasters.com/blog Helping Your Journey to Senior Developer Thu, 04 Jul 2024 14:14:49 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 225069128 New JavaScript Set methods https://frontendmasters.com/blog/new-javascript-set-methods/ https://frontendmasters.com/blog/new-javascript-set-methods/#respond Thu, 04 Jul 2024 14:14:48 +0000 https://frontendmasters.com/blog/?p=2928 Sets in JavaScript are quite useful as they are like an array, but can only contain one of a specific item. So you can unique-ify an array easily, and even willy-nilly add stuff to it and not worry about duplicates:

const people = ["chris", "chris", "cindy"];
const peopleSet = new Set(people);
peopleSet.add("chris");
console.log(peopleSet);
// {"chris","cindy"}

Now they are even more useful with recently universally available methods, as Brian Smith summarizes:

  • intersection() returns a new set with elements in both this set and the given set.
  • union() returns a new set with all elements in this set and the given set.
  • difference() returns a new set with elements in this set but not in the given set.
  • symmetricDifference() returns a new set with elements in either set, but not in both.
  • isSubsetOf() returns a boolean indicating if all elements of a set are in a specific set.
  • isSupersetOf() returns a boolean indicating if all elements of a set are in a specific set.
  • isDisjointFrom() returns a boolean indicating if this set has no elements in common with a specific set.
]]>
https://frontendmasters.com/blog/new-javascript-set-methods/feed/ 0 2928