DEV Community

Veranika Kasparevych
Veranika Kasparevych

Posted on • Updated on

"That's what she Set!" - mastering data with 7 new JavaScript Set methods

In this article, I will be spilling the beans on the coolest, latest methods that probably will turn the Set object into a rockstar. I noticed these new methods in July and finally was able to test them in Safari. All of them are fresh ways to play with data, do crazy intersections, and make unions so smooth you'll wonder how you ever lived without them.

So, 7 new Set methods are under construction now and I will show you the way they interact with data:

  • difference()
  • intersection()
  • isDisjointFrom()
  • isSubsetOf()
  • isSupersetOf()
  • symmetricDifference()
  • union()

Now, let's delve into these methods and see how they elevate the power of Sets in handling data.

We will be working on these two wonderful Sets containing the employees of two best offices of the Dunder Mifflin paper company:


const scrantonOfficeEmployees = new Set([ "Michael Scott", "Jim Halpert", "Pam Beesly", "Dwight Schrute", "Angela Martin", "Stanley Hudson", "Phyllis Smith", "Ryan Howard", "Kelly Kapoor", "Oscar Martinez", "Kevin Malone", "Meredith Palmer", "Creed Bratton", "Toby Flenderson", "Jan Levinson" ]);


const stamfordOfficeEmployees = new Set([ "Jim Halpert", "Andy Bernard", "Karen Filippelli", "Josh Porter", "Tony Gardner" ]);


const dunderMifflinEmployees = new Set([...scrantonOfficeEmployees, ...stamfordOfficeEmployees]); 

Enter fullscreen mode Exit fullscreen mode
  • The difference() method of Set instances takes a set and returns a new set containing elements in this set but not in the given set:

dunderMifflinEmployees.difference(scrantonOfficeEmployees);

// Output: Set (4) {"Andy Bernard", "Karen Filippelli", "Josh Porter", "Tony Gardner"}

Enter fullscreen mode Exit fullscreen mode
  • The intersection() method of Set instances takes a set and returns a new set containing elements in both this set and the given set:

stamfordOfficeEmployees.intersection(scrantonOfficeEmployees);  

// Output: Set (1) {"Jim Halpert"}

Enter fullscreen mode Exit fullscreen mode

  • The symmetricDifference() method of Set instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both:

stamfordOfficeEmployees.symmetricDifference(scrantonOfficeEmployees);  

// Output:  Set (18) {"Andy Bernard", "Karen Filippelli", "Josh Porter", "Tony Gardner", "Michael Scott", "Pam Beesly", "Dwight Schrute", "Angela Martin", "Stanley Hudson", "Phyllis Smith", "Ryan Howard", "Kelly Kapoor", "Oscar Martinez", "Kevin Malone", "Meredith Palmer", "Creed Bratton", "Toby Flenderson", "Jan Levinson"}

Enter fullscreen mode Exit fullscreen mode
  • The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set:

stamfordOfficeEmployees.union(scrantonOfficeEmployees);  

// Output: Set (19) {"Jim Halpert", "Andy Bernard", "Karen Filippelli", "Josh Porter", "Tony Gardner", "Michael Scott", "Pam Beesly", "Dwight Schrute", "Angela Martin", "Stanley Hudson", "Phyllis Smith", "Ryan Howard", "Kelly Kapoor", "Oscar Martinez", "Kevin Malone", "Meredith Palmer", "Creed Bratton", "Toby Flenderson", "Jan Levinson"}

Enter fullscreen mode Exit fullscreen mode
  • The isSubsetOf() method of Set instances takes a set and returns a boolean indicating if all elements of this set are in the given set:

new Set(["Michael Scott", "Jim Halpert"]).isSubsetOf(scrantonOfficeEmployees);  

// Output: true  

Enter fullscreen mode Exit fullscreen mode
  • The isSupersetOf() method of Set instances takes a set and returns a boolean indicating if all elements of the given set are in this set:

scrantonOfficeEmployees.isSupersetOf(new Set(["Michael Scott", "Jim Halpert"])); 

// Output: true  

Enter fullscreen mode Exit fullscreen mode
  • The isDisjointFrom() method of Set instances takes a set and returns a boolean indicating if this set has no elements in common with the given set:

stamfordOfficeEmployees.isDisjointFrom(dunderMifflinEmployees);  

// Output: false  

Enter fullscreen mode Exit fullscreen mode

Stay tuned for further updates and enhancements to the Set object's capabilities. You can keep track of all the details on the official Mozilla documentation.

Top comments (2)

Collapse
 
piko profile image
Piko

Great work!

Collapse
 
veranikabarel profile image
Veranika Kasparevych

Thank you :)