Iterators

Champion: Dave Herman
Details: ES Wiki

A object that represents a collection can provide an [iterator]() method so that other code can conveniently iterate over its contents.

  • An iterable object is any object that has an [iterator]() method that returns an iterator. (iterator is a standard private name.)

  • An iterator is any object that has a .next() method. This method returns the next item, if any, and throws a StopIteration object if there are no more items.

  • The easiest way to produce an iterator is to write a generator. The easiest way to consume an iterator is to write a for-of loop.

The design of iterators and generators in ES6 follows similar features in Python.

Examples

The simplest way to make an object iterable is to give it an [iterator] method that is a generator.

    // Make jQuery objects iterable
    import iterator from "@iter";
    jQuery.prototype[iterator] = function* iterator() {
        for (var i = 0; i < this.length; i++)
            yield $(this.get(i));
    };

    // Example code looping over a jQuery collection
    var t = 500;
    for (var p of $("p")) {
        p.fadeOut(t);
        t += 200;
    }

The standard @iter module provides a few convenience functions for iterating over object property names and values:

    import keys, values, items from "@iter";

    for (var k of keys(obj))
        alert("property name: " + k);

    for (var v of values(obj))
        alert("property value: " + v);

    for (var [k, v] of items(obj))
        alert(k + ": " + v);

A few builtin functions can operate on any iterable object. For example, pass any iterable object to Set() and it will read all the values out of that iterable object and return a set of those values:

```javascript var data = [1, 2, 1, 3, 1, 2, 1]; var s = Set(data); // A set with the three values 1, 2, 3