In ES6, there is iterator.next(); is there any way to supply iterator.previous()?
The values()
returns an iterator object and it is not possible to iterate them backwards, because JavaScript iterator objects could be infinite. For example, consider this
function * Counter() {
"use strict";
var i = 0;
while (1) {
yield i++;
}
}
Now, you can create an iterator with Counter()
and that will never end. So going backwards is not an option with iterators, generally.
If you badly need something like backIterator, then you have to maintain the values produced by the iterator and then move back and forth based on the next
calls.