Set
Explore the ES6 Set object to store unique values without keys. Learn methods like add, has, forEach, delete, clear, and how to iterate over Sets. Understand WeakSet for object-only collections and managing references.
We'll cover the following...
We'll cover the following...
In the previous chapter we looked at the Map object, and how we can use it to store key/value pairs. In this chapter we will look at Set and how to store unique values.
A Set is created in the same way as Map.
add()
With a Set we can use the .add() method to add elements to the Set. This is where a Map and Set differ: there are no keys in a Set.
Adding multiple values
When we create a new Set we can pass in an array of values for our Set.
has()
The .has method can be used to check if a value exists in the Set. ...