Created
July 19, 2017 03:50
-
-
Save arkilis/3e18e939aa527c41a4c3acbe5bf918e9 to your computer and use it in GitHub Desktop.
manual iterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add iterator to user object | |
let user = { | |
name: "sam", totalReplies: 17, isBlocked: false | |
}; | |
user[Symbol.iterator] = function(){ | |
let properties = Object.keys(this); | |
let count = 0; | |
let isDone = false; | |
let next = () => { | |
if(count >= properties.length){ | |
isDone = true; | |
} | |
let value = this[properties[count++]]; | |
return { done: isDone, value:{value} }; | |
}; | |
return { next }; | |
}; | |
// iterate user object | |
let values = [...user]; | |
console.log(values); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment