7 New JavaScript Set Methods

TL;DR: JavaScript’s Set data structure has added 7 new APIs that are very useful and can reduce the need for libraries like lodash. They are:

intersection() returns a new set with elements in both this set and the given set.
union() returns a new set…


This content originally appeared on DEV Community and was authored by Zachary Lee

TL;DR: JavaScript’s Set data structure has added 7 new APIs that are very useful and can reduce the need for libraries like lodash. They are:

  • 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.

1. Set.prototype.intersection()

The intersection() method of Set instances takes a set and returns a new set containing elements in both this set and the given set.

Venn diagram:

Example:

2. Set.prototype.union()

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.

Venn diagram:

Example:

3. Set.prototype.difference()

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.

Venn diagram:

Example:

4. Set.prototype.symmetricDifference()

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.

Venn diagram:

Example:

5. Set.prototype.isSubsetOf()

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.

Venn diagram:

Example:

6. Set.prototype.isSupersetOf()

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.

Venn diagram:

Example:

7. Set.prototype.isDisjointFrom()

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.

Venn diagram:

Example:

If you find my content helpful, please consider subscribing. I send a weekly newsletter every Sunday with the latest web development updates. Thanks for your support!


This content originally appeared on DEV Community and was authored by Zachary Lee


Print Share Comment Cite Upload Translate Updates
APA

Zachary Lee | Sciencx (2024-07-13T14:38:14+00:00) 7 New JavaScript Set Methods. Retrieved from https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/

MLA
" » 7 New JavaScript Set Methods." Zachary Lee | Sciencx - Saturday July 13, 2024, https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/
HARVARD
Zachary Lee | Sciencx Saturday July 13, 2024 » 7 New JavaScript Set Methods., viewed ,<https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/>
VANCOUVER
Zachary Lee | Sciencx - » 7 New JavaScript Set Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/
CHICAGO
" » 7 New JavaScript Set Methods." Zachary Lee | Sciencx - Accessed . https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/
IEEE
" » 7 New JavaScript Set Methods." Zachary Lee | Sciencx [Online]. Available: https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/. [Accessed: ]
rf:citation
» 7 New JavaScript Set Methods | Zachary Lee | Sciencx | https://www.scien.cx/2024/07/13/7-new-javascript-set-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.