Keys, Values, Entries ( ) Methods
A plain object cannot be looped over with for...of or given to array methods such as map and filter. Object.keys(), Object.values() and Object.entries() solve this by returning the object’s properties as an array, which can then be looped over or transformed like any other array.
The three methods
Section titled “The three methods”Object.keys(obj)returns an array of the property names.Object.values(obj)returns an array of the property values.Object.entries(obj)returns an array of[name, value]pairs.
const stock = { apples: 12, pears: 0, plums: 7 };
console.log(Object.keys(stock));console.log(Object.values(stock));console.log(Object.entries(stock));This prints:
[ 'apples', 'pears', 'plums' ][ 12, 0, 7 ][ [ 'apples', 12 ], [ 'pears', 0 ], [ 'plums', 7 ] ]The arrays are new copies, so changing them does not change the object.
Looping and totalling
Section titled “Looping and totalling”Object.entries() pairs well with for...of and array destructuring, which unpacks each pair into two variables. Object.values() is the quick route to a total:
const stock = { apples: 12, pears: 0, plums: 7 };
for (const [fruit, count] of Object.entries(stock)) { console.log(`${fruit}: ${count}`);}
const total = Object.values(stock).reduce((sum, n) => sum + n, 0);console.log(`Total: ${total}`);This prints:
apples: 12pears: 0plums: 7Total: 19Back to an object with Object.fromEntries()
Section titled “Back to an object with Object.fromEntries()”Object.fromEntries() does the reverse of Object.entries(), so it builds an object from an array of pairs. Together they let an object be mapped or filtered with array methods:
const prices = { apples: 0.4, pears: 0.55, plums: 0.3 };
const withVat = Object.fromEntries( Object.entries(prices).map(([fruit, price]) => [fruit, +(price * 1.2).toFixed(2)]));console.log(withVat);
const inStock = Object.fromEntries( Object.entries({ apples: 12, pears: 0, plums: 7 }).filter(([, count]) => count > 0));console.log(inStock);This prints:
{ apples: 0.48, pears: 0.66, plums: 0.36 }{ apples: 12, plums: 7 }The empty slot in [, count] skips the name, because only the count is needed.
Which properties are included, and in what order
Section titled “Which properties are included, and in what order”All three methods include only the object’s own enumerable properties with string names. Inherited properties, properties defined as not enumerable and properties named by symbols are left out, which is the main difference from a for...in loop, since for...in also visits inherited enumerable properties.
The order is fixed by the language: names that look like whole numbers come first in ascending numeric order, and the other names follow in the order they were added:
const base = { shared: true };const box = Object.create(base);box.b = 'second';box[10] = 'ten';box.a = 'third';box[2] = 'two';Object.defineProperty(box, 'hidden', { value: 'secret', enumerable: false });
console.log(Object.keys(box));This prints:
[ '2', '10', 'b', 'a' ]shared is inherited and hidden is not enumerable, so neither appears. Where the order of insertion matters for every kind of key, a Map keeps it exactly.
Steve Griffith covers this topic in his video Object keys, values, and entries methods.