Last active
November 7, 2020 23:36
-
-
Save kentcdodds/0cad1a7ba98ea6098334720aed7fdc46 to your computer and use it in GitHub Desktop.
Example of using Object.entries
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
// http://www.dogbreedchart.com | |
const dogShedding = { | |
foxhound: 3, | |
'basset hound': 2, | |
beagle: 3, | |
'bernese mountain dog': 1, | |
poodle: 5, | |
} | |
// π₯π₯ | |
// Object.keys is an ES 5.1 feature (supported in IE9) | |
Object.keys(dogShedding).forEach(key => { | |
const value = dogShedding[key] | |
// etc. | |
}) | |
// We've been using Object.keys to do this for a while... | |
// π₯π₯π₯π₯ | |
// Just need the value? Save yourself some trouble | |
// and use Object.values instead! | |
Object.values(dogShedding).forEach(value => { | |
// etc. | |
}) | |
// Object.values is an ES2017 feature (polyfillable in IE) | |
// π₯π₯π₯π₯π₯π₯π₯π₯ | |
// Need both key and value? Use Object.entries and destructuring | |
Object.entries(dogShedding).forEach(([key, value]) => { | |
// etc. | |
}) | |
// Object.entries is also ES2017 feature (polyfillable in IE) | |
// destructuring is an ES2015 feature and transpileable by | |
// Babel with babel-preset-env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Comment via twitter: https://twitter.com/kentcdodds/status/972179744186871808