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

4.4 Iterators

Iterators aren't data structures themselves, but they are often used to work with data structures such as sets and maps. Let's learn about the new iteration protocol and see how to iterate some of the built-in JavaScript data structures.

4.4 Iterators

Hi, folks. In this lesson, we're going to take a look at iterators. We've mentioned the fact that both sets and maps have methods that return iterators. So now, it's time to take a look at what an iterator actually is. An iterator is an object that provides access to a collection of items one at a time and which keeps track of which items have been accessed. In ES6, an Iterator is an object that contains a next() method. When the next() method is invoked, an object is returned with two keys, value and done. Value will be whatever value the Iterator returned from the collection on that invocation of next, while done is a Boolean that equals true only when the Iterator has finished iterating the collection. So, let's take a look at some iterators. One of the sets methods that returns an iterator is the entries method. In this case, items is now an iterator, so we can invoke it's next() method in order to pull out the first value. The next method returns an object with two values, done and value. Done is false this time, because we still have values to get. In order to keep sets and maps consistent, the value is an array. Because the values in a set don't have keys, both items in this array are the same, the current value. When we use the entries method with a map instead of a set, the value array in the object returned by next contains both the key and the value. In this case, the value array contains the key, which for the first item in the map is the string "a" and the second item in the array is the value associated with that key, which in this case is the number one. Again, dumb is false, because there are still items in the map that haven't been iterated yet. To get the next value from either of items or map items iterators, we just invoke next() again. And eventually, once the end of the collection is reached, the object returned by next() will have true as the done value. The map has two items inside it. But even after we've invoked the next method for the second time and received the second object from the map, done is still false. We have to call the next method one more time in order to receive true as the value is done. And in this case, the value that gets returned is undefined, because we've already had all of the values pulled out. As well as the entries method for returning an iterator, both sets and maps also have method called values which returns an iterator. This iterator behaves slightly differently in that the value property of the object returned by the next method is a single value, instead of an array. So in this case, value is a single Item. A map has one more iterator returning method, the keys method. Sets don't have access to this method, because in a set, there are no keys. The iterator that the keys method returns works in the same way that the values method works, except that it returns each key in the map instead of each value. Other powers of JavaScript can also return iterators like generator functions, which we'll be looking at in the next section. So in this lesson, we look at iterators. An iterator is any object that exposes a next method. When this method is invoked, an object is returned, which contains the current value in the collection the iterator is operating on and a Boolean indicating whether there are more iterators in the collection. Iterators are easy to work with and represent a useful pattern for iterating collections of items. Thanks for watching.

Back to the top