Skip to content

Instantly share code, notes, and snippets.

@Ibro Ibro/iterators.js
Last active Apr 26, 2017

Embed
What would you like to do?
JavaScript Iterators - Coding Blast - www.codingblast.com
function makeIterator(array) {
let index = 0;
return {
next: function () {
if (index < array.length) {
return { value: array[index++], done: false };
} else {
return { done: true };
}
}
};
}
let it = makeIterator(['coding', 'blast']);
console.log(it.next()); // [object Object] { done: false, value: "coding" }
console.log(it.next()); // [object Object] { done: false, value: "blast" }
console.log(it.next()); // [object Object] { done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.