Skip to content

Instantly share code, notes, and snippets.

@bbarrows
Created January 26, 2017 01:36
Show Gist options
  • Save bbarrows/54206d599b9d84ebad5d6b60c46f4211 to your computer and use it in GitHub Desktop.
Save bbarrows/54206d599b9d84ebad5d6b60c46f4211 to your computer and use it in GitHub Desktop.
var arr = [4,5,6,7,8,9];
for (var v of arr) {
console.log( v );
}
// 4 5 6 7 8 9
// define iterator that only produces values
// from odd indexes
arr[Symbol.iterator] = function*() {
var idx = 1;
do {
yield this[idx];
} while ((idx += 2) < this.length);
};
for (var v of arr) {
console.log( v );
}
// 5 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment