Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Ecma 1
  • Overview
  • Transcript

4.1 Sets

Set is a new JavaScript built-in data structure that stores a collection of unique objects. Only one instance of each object will be present in the set—no duplicates allowed! In this lesson, I'll show you how to get started using sets.

4.1 Sets

Hi, folks. In this lesson, we're going to look at a new type of data structure in ES6, the set. A set is a data structure that we can use to store unique values in. The values can be of any type, but they must still be unique. Babel supports the set through its polyfill. So we'll continue using this and the es6.js file like we did in the last section. To create a set, we use the set constructor. We can optionally provide an iterable object as an argument to the constructor, and all of the values will be added to the set. This can be useful for removing duplicates from an array. One point to note about sets is that nan is treated as same as nan, unlike in strict equality comparisons. To see how many values are in a set, we can use the size property. This method returns and integer indicating how many items there are in the sets. We also have a range of different methods that we can use to work with sets. We can add or remove values using the add and remove methods respectively. We can only add a single value at a time. But as the method returns the set, we can chain it. To remove a value, we use the delete method, and pas the value that we'd like to remove, as a parameter. This method returns a boolean to indicate whether the value was removed. If we try to remove a value that doesn't exist, nothing bad will happen, the method will still return false. If we want to remove all of the values from the set, we can use the clear method instead. This method doesn't take any parameters and always returns undefined. If we want to determine whether a particular value exists in a set, we can use the has method In this case, the method returned false, because we've just cleared the set, and it doesn’t contain anything at this point. We've got a couple of different ways of iterating the values in a set. We can use the entries or values methods, which both return an iterator. We'll look at iterators in more detail later in the course. We can also use the for each method on a set. So let's have a quick look at that now. In order to keep the behavior of the callback function the same whether the for each method is called on a set, a map, or an array, the callback function is still past three arguments. But the first two are the same. The third argument is the set the method is called on. While a set may seem conceptually similar to an array, we should remember that it isn't. Values don't have an index or key, so we can't pull out an individual value using its index. Trying to do this simply results in undefined. So in this lesson we looked at a new set objects in ES6. We saw that it is a data structure that allows us to store unique values. We saw that we can get the number of values in the set using the size property. And we looked at some of the methods we can invoke on a set, including add, delete, clear, has and for each. Thanks for watching.

Back to the top