Skip to content

Instantly share code, notes, and snippets.

@leonardreidy
Last active September 30, 2019 08:12
Show Gist options
  • Save leonardreidy/d323848925a5695a7d11a3bf64e1483b to your computer and use it in GitHub Desktop.
Save leonardreidy/d323848925a5695a7d11a3bf64e1483b to your computer and use it in GitHub Desktop.
function iteratorFactory(obj) {
var nextIndex = 0;
var keys = Object.keys(obj);
var end = keys.length;
var next = function(){
return nextIndex < end ? { value: { [keys[nextIndex]]: obj[keys[nextIndex++]]}, done: false } : { value: undefined, done: true }
}
return { next: next };
}
let ob = { age: 41, name: 'Leonard', profession: 'JavaScript Developer' };
let obit = iteratorFactory(ob);
obit.next(); // returns { value: { age: 41 }, done: false }
obit.next(); // returns { value: { name: 'Leonard'}, done: false }
obit.next(); // returns { value: { profession: 'JavaScript Developer'}, done: false }
obit.next(); // returns { value: undefined, done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment