Created
November 22, 2016 10:17
-
-
Save Deliaz/aa9a9d764606b0a3ab790a2f6bd0dff3 to your computer and use it in GitHub Desktop.
ES6 object itareraton
This file contains hidden or 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
// using a generator function | |
function* entries(obj) { | |
for (let key of Object.keys(obj)) { | |
yield [key, obj[key]]; | |
} | |
} | |
// an alternative version using a generator expression | |
function entries(obj) { | |
return (for (key of Object.keys(obj)) [key, obj[key]]); | |
} | |
for (let [key, value] of entries(myObj)) { | |
// do something with key|value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment