Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Last active November 7, 2020 23:36
Show Gist options
  • Save kentcdodds/0cad1a7ba98ea6098334720aed7fdc46 to your computer and use it in GitHub Desktop.
Save kentcdodds/0cad1a7ba98ea6098334720aed7fdc46 to your computer and use it in GitHub Desktop.
Example of using Object.entries
// 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
@kentcdodds
Copy link
Author

kentcdodds commented Mar 9, 2018

@anthonybrown
Copy link

Good stuff @kentdodds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment