Skip to content

Instantly share code, notes, and snippets.

@73nko
Last active May 14, 2019 10:31
Show Gist options
  • Save 73nko/5e956e44de5522797729d4d6c12ba8bf to your computer and use it in GitHub Desktop.
Save 73nko/5e956e44de5522797729d4d6c12ba8bf to your computer and use it in GitHub Desktop.
Reverse loop array with iterables.
class IterableBackwards {
constructor(xs) {
this._xs = xs;
this._i = xs.length;
}
[Symbol.iterator]() { return this; }
next() {
return (this._i <= 0)
? { done: true, value: undefined }
: { done: false, value: this._xs[--this._i] };
}
}
const xs = ['A1', 'A2', 'A3'];
for (const x of new IterableBackwards(xs)) {
console.info(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment