ES8 Object.entries/values

examples.dev
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.

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

examples.dev

Written by

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade