10.1. next() over an array, one value per call
Topics
This question is in
10. Proxies, iterators and generatorsfor...of, spread ([...list]) and Array.from all read an array the same way: they ask it for an iterator, then call the iterator's next() again and again. Each call hands out one item, wrapped as { value, done: false }, until a call answers { value: undefined, done: true }. Writing that object yourself is the clearest way to see what every loop over an array is doing underneath, and it is the shape you need whenever you make your own data iterable.
Write createIterator(array) in iterator.js. It returns an iterator that walks array from the first item to the last, one item per next() call, the way the built-in array iterator does.
Signature
function createIterator(array) // returns an iterator objectarray: an array, or any array-like object (an object with alengthand items at0,1, …).- Returns an object with a
next()method and a[Symbol.iterator]()method. next()returns a new object{ value, done }on every call.
Requirements
- Each
next()returns the next item as{ value: item, done: false }, in index order, starting at index 0. - After the last item,
next()returns exactly{ value: undefined, done: true }, with both keys. - Once it has returned
done: true, it keeps returning{ value: undefined, done: true }on every later call, even if items are added to the array afterwards. - Items that are
undefined,null,0,""orfalse, and holes in a sparse array, still count as items:doneisfalsefor them (a hole'svalueisundefined). - The iterator reads
array.lengthon every call, so an item pushed before the end is handed out too. - The array is never changed: no item is removed or moved.
- Each call to
createIteratorhas its own position; two iterators over the same array do not move each other. - Every
next()returns a fresh object, never the same object changed. - The iterator is iterable:
[Symbol.iterator]()returns the iterator itself, sofor...ofand spread work on it. - It works on array-like objects too.
- It is a plain object with a
nextmethod, not a generator, and it does not use the built-in array iterator.
Examples
const it = createIterator(["a", "b"]);
it.next(); // { value: "a", done: false }
it.next(); // { value: "b", done: false }
it.next(); // { value: undefined, done: true }
it.next(); // { value: undefined, done: true }const holes = createIterator([1, , 3]);
holes.next(); // { value: 1, done: false }
holes.next(); // { value: undefined, done: false }: a hole is still an item
holes.next(); // { value: 3, done: false }const list = [1, 2];
const it = createIterator(list);
it.next(); // { value: 1, done: false }
list.push(3);
[...it]; // [2, 3]: spread finishes what next() started
createIterator({ length: 2, 0: "x", 1: "y" }).next(); // { value: "x", done: false }Environment
- Plain JavaScript in a browser page; the tests call the global
createIteratorfromiterator.js. - Do not use a generator function (
function*) or the array's own iterator (array[Symbol.iterator](),values(),keys(),entries()). The tests replace the built-in array iterator with one that throws while they run. - A
return()method (closing the iterator early) is not part of this problem.
Hints
- Which two pieces of state must survive from one
next()call to the next? - Requirement 3 says "keeps returning done", even after a push. Is checking
index < array.lengthalone enough for that?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it