
ES8 Object.entries/values
Jul 10, 2017 · 1 min read
With the release of ES8 (a.k.a 2017) we now have Object.values and Object.entries to accompany Object.keys. In this article, I will give a brief overview of their usage.
Using Object.keys to iterate over JavaScript object’s keys.
const countries = {
FJ: 'Fiji',
CL: 'Chile',
}
Object.keys(countries) // ['FJ', 'CL']Now we can do the same for values.
const countries = {
FJ: 'Fiji',
CL: 'Chile',
}
Object.values(countries) // ['Fiji', 'Chile']But what happens if you would like to do both at the same time?
const countries = {
FJ: 'Fiji',
CL: 'Chile',
}
Object.entries(countries) // [['FJ', 'Fiji'], ['CL', 'Chile']]Let’s map over the countries using template strings and array destructuring.
const countries = {
FJ: 'Fiji',
CL: 'Chile',
}
Object.entries(countries).map(([code, name]) => `${name} (${code})`) // ['Fiji (FJ)', 'Chile (CL)']Object.values and Object.entries are both available in all modern browsers and node 8.
Originally published at examples.dev.

