Hiprup

What is Object.keys(), Object.values(), and Object.entries()?

Three methods that turn an object's own properties into arrays so you can iterate them.

  • Object.keys(obj) — an array of the property names.

  • Object.values(obj) — an array of the property values.

  • Object.entries(obj) — an array of [key, value] pairs, ideal for looping or building a Map.

Note: they return only own, enumerable properties — inherited ones are skipped.

const user = { name: 'John', age: 30, email: 'john@test.com' };

console.log(Object.keys(user));    // ['name', 'age', 'email']
console.log(Object.values(user));  // ['John', 30, 'john@test.com']
console.log(Object.entries(user)); // [['name','John'], ['age',30], ['email','john@test.com']]

// Practical: iterate object
for (const [key, value] of Object.entries(user)) {
  console.log(`${key}: ${value}`);
}

// Convert entries back to object
const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries); // { a: 1, b: 2 }

// Transform object values
const prices = { apple: 1.5, banana: 0.75 };
const discounted = Object.fromEntries(
  Object.entries(prices).map(([k, v]) => [k, v * 0.9])
); // { apple: 1.35, banana: 0.675 }

keys returns property names, values returns property values, entries returns [key, value] pairs. Object.entries with for...of enables clean object iteration.

Object.fromEntries converts [key, value] pairs back to an object. The price transformation shows a practical pattern: entries → map → fromEntries.

Know all three and when to use each. Object.entries + for...of is the cleanest object iteration.

Object.fromEntries for transforming objects (entries → map → fromEntries) is a powerful pattern. These only return OWN ENUMERABLE STRING-KEYED properties.

What is Object.keys(), Object.values(), and Object.entries()? | Hiprup