JS Object Keys, Values, and Entries Full Guide

Master Object.keys(), Object.values(), and Object.entries() in JavaScript. Learn how to iterate over objects, transform data, filter properties, and convert between objects and arrays with practical examples.

JavaScriptbeginner
14 min read

JavaScript objects do not have built-in array methods like map, filter, or reduce. To iterate over and transform object data, you first need to convert the object into an array using Object.keys(), Object.values(), or Object.entries(). These three methods are among the most frequently used tools in JavaScript development, powering everything from data transformation to UI rendering.

This guide covers all three methods with practical examples, shows how to combine them with array methods, and teaches common patterns for working with object data.

Object.keys()

Object.keys() returns an array of an object's own enumerable string property names:

javascriptjavascript
const user = {
  name: "Alice",
  age: 28,
  email: "alice@example.com",
  role: "developer"
};
 
const keys = Object.keys(user);
console.log(keys);
// ["name", "age", "email", "role"]

Counting Properties

javascriptjavascript
const product = {
  name: "Laptop",
  price: 999,
  brand: "TechBrand",
  inStock: true
};
 
console.log(Object.keys(product).length); // 4

Checking for Empty Objects

javascriptjavascript
function isEmpty(obj) {
  return Object.keys(obj).length === 0;
}
 
console.log(isEmpty({}));           // true
console.log(isEmpty({ a: 1 }));    // false
console.log(isEmpty(new Object())); // true

Iterating with Object.keys

javascriptjavascript
const scores = {
  math: 92,
  science: 88,
  english: 95,
  history: 78
};
 
Object.keys(scores).forEach(subject => {
  console.log(`${subject}: ${scores[subject]}`);
});
// math: 92
// science: 88
// english: 95
// history: 78

Object.values()

Object.values() returns an array of an object's own enumerable property values:

javascriptjavascript
const user = {
  name: "Alice",
  age: 28,
  email: "alice@example.com"
};
 
const values = Object.values(user);
console.log(values);
// ["Alice", 28, "alice@example.com"]

Calculating with Values

javascriptjavascript
const monthlyExpenses = {
  rent: 1500,
  food: 400,
  transport: 120,
  utilities: 200,
  entertainment: 150
};
 
// Sum all values
const total = Object.values(monthlyExpenses).reduce((sum, cost) => sum + cost, 0);
console.log(`Total: $${total}`); // "Total: $2370"
 
// Find highest expense
const highest = Math.max(...Object.values(monthlyExpenses));
console.log(`Highest: $${highest}`); // "Highest: $1500"
 
// Average expense
const avg = total / Object.values(monthlyExpenses).length;
console.log(`Average: $${avg.toFixed(2)}`); // "Average: $474.00"

Checking if a Value Exists

javascriptjavascript
const permissions = {
  read: true,
  write: true,
  delete: false,
  admin: false
};
 
const hasAnyEnabled = Object.values(permissions).some(v => v === true);
console.log(hasAnyEnabled); // true
 
const allEnabled = Object.values(permissions).every(v => v === true);
console.log(allEnabled); // false

Object.entries()

Object.entries() returns an array of [key, value] pairs, which is the most versatile of the three methods:

javascriptjavascript
const user = {
  name: "Alice",
  age: 28,
  role: "developer"
};
 
const entries = Object.entries(user);
console.log(entries);
// [["name", "Alice"], ["age", 28], ["role", "developer"]]

Iterating with Destructuring

Object.entries pairs perfectly with destructuring in loops:

javascriptjavascript
const inventory = {
  laptops: 45,
  phones: 120,
  tablets: 30,
  monitors: 65
};
 
for (const [product, count] of Object.entries(inventory)) {
  console.log(`${product}: ${count} units`);
}
// laptops: 45 units
// phones: 120 units
// tablets: 30 units
// monitors: 65 units

Filtering Object Properties

javascriptjavascript
const scores = {
  Alice: 92,
  Bob: 65,
  Charlie: 88,
  Diana: 45,
  Eve: 97
};
 
// Keep only passing scores (>= 70)
const passing = Object.fromEntries(
  Object.entries(scores).filter(([name, score]) => score >= 70)
);
 
console.log(passing);
// { Alice: 92, Charlie: 88, Eve: 97 }

Transforming Object Data

javascriptjavascript
const prices = {
  apple: 1.50,
  banana: 0.75,
  orange: 2.00,
  mango: 3.50
};
 
// Apply 20% discount to all prices
const discounted = Object.fromEntries(
  Object.entries(prices).map(([fruit, price]) => [fruit, +(price * 0.8).toFixed(2)])
);
 
console.log(discounted);
// { apple: 1.2, banana: 0.6, orange: 1.6, mango: 2.8 }

Comparison of All Three Methods

MethodReturnsBest For
Object.keys(obj)string[]Counting properties, checking existence, key-based iteration
Object.values(obj)any[]Calculations, aggregations, value-based checks
Object.entries(obj)[string, any][]Full iteration, transformation, filtering, conversion
javascriptjavascript
const product = {
  name: "Laptop",
  price: 999,
  inStock: true
};
 
console.log(Object.keys(product));    // ["name", "price", "inStock"]
console.log(Object.values(product));  // ["Laptop", 999, true]
console.log(Object.entries(product)); // [["name", "Laptop"], ["price", 999], ["inStock", true]]

Object.fromEntries()

Object.fromEntries() is the reverse of Object.entries(). It converts an array of [key, value] pairs back into an object:

javascriptjavascript
const entries = [
  ["name", "Alice"],
  ["age", 28],
  ["role", "developer"]
];
 
const user = Object.fromEntries(entries);
console.log(user);
// { name: "Alice", age: 28, role: "developer" }

Converting Map to Object

javascriptjavascript
const userMap = new Map([
  ["name", "Alice"],
  ["age", 28],
  ["email", "alice@example.com"]
]);
 
const userObj = Object.fromEntries(userMap);
console.log(userObj);
// { name: "Alice", age: 28, email: "alice@example.com" }

entries + fromEntries Pipeline

The entries → transform → fromEntries pipeline is the standard pattern for transforming objects:

javascriptjavascript
const rawData = {
  "  Name  ": "  Alice  ",
  " Email": " alice@example.com ",
  "Role  ": "  developer "
};
 
// Clean up all keys and values
const cleaned = Object.fromEntries(
  Object.entries(rawData).map(([key, value]) => [
    key.trim().toLowerCase(),
    typeof value === "string" ? value.trim() : value
  ])
);
 
console.log(cleaned);
// { name: "Alice", email: "alice@example.com", role: "developer" }

Practical Patterns

Grouping Array Items into an Object

javascriptjavascript
const orders = [
  { id: 1, category: "electronics", total: 299 },
  { id: 2, category: "clothing", total: 49 },
  { id: 3, category: "electronics", total: 599 },
  { id: 4, category: "food", total: 25 },
  { id: 5, category: "clothing", total: 89 }
];
 
const grouped = orders.reduce((acc, order) => {
  const key = order.category;
  if (!acc[key]) acc[key] = [];
  acc[key].push(order);
  return acc;
}, {});
 
// Summary per category
const summary = Object.fromEntries(
  Object.entries(grouped).map(([category, items]) => [
    category,
    {
      count: items.length,
      total: items.reduce((sum, o) => sum + o.total, 0)
    }
  ])
);
 
console.log(summary);
// { electronics: { count: 2, total: 898 }, clothing: { count: 2, total: 138 }, food: { count: 1, total: 25 } }

Inverting an Object (Swap Keys and Values)

javascriptjavascript
const statusCodes = {
  200: "OK",
  404: "Not Found",
  500: "Internal Server Error",
  403: "Forbidden"
};
 
const statusNames = Object.fromEntries(
  Object.entries(statusCodes).map(([code, name]) => [name, Number(code)])
);
 
console.log(statusNames);
// { OK: 200, "Not Found": 404, "Internal Server Error": 500, Forbidden: 403 }

Picking Specific Properties

javascriptjavascript
function pick(obj, keys) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key]) => keys.includes(key))
  );
}
 
function omit(obj, keys) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key]) => !keys.includes(key))
  );
}
 
const user = {
  id: 42,
  name: "Alice",
  email: "alice@example.com",
  password: "secret",
  role: "admin"
};
 
console.log(pick(user, ["name", "email"]));
// { name: "Alice", email: "alice@example.com" }
 
console.log(omit(user, ["password", "role"]));
// { id: 42, name: "Alice", email: "alice@example.com" }

Sorting an Object by Values

javascriptjavascript
const scores = {
  Alice: 92,
  Bob: 65,
  Charlie: 88,
  Diana: 97,
  Eve: 78
};
 
const sorted = Object.fromEntries(
  Object.entries(scores).sort(([, a], [, b]) => b - a)
);
 
console.log(sorted);
// { Diana: 97, Alice: 92, Charlie: 88, Eve: 78, Bob: 65 }

Property Order

Objects in JavaScript maintain insertion order for string keys (with numeric-like keys sorted first):

javascriptjavascript
const mixed = { b: 2, a: 1, "2": "two", "1": "one", c: 3 };
 
console.log(Object.keys(mixed));
// ["1", "2", "b", "a", "c"]
// Numeric keys come first (sorted), then string keys in insertion order

Best Practices

  1. Use Object.entries as your default when you need both keys and values
  2. Combine with array methods: entries → map/filter/reduce → fromEntries is the standard transformation pipeline
  3. Prefer Object.keys().length over for...in loops for counting properties
  4. Use destructuring in entries callbacks: ([key, value]) => instead of (entry) => entry[0]
  5. Use Object.fromEntries() to convert transformed entries back to objects cleanly

Common Mistakes to Avoid

Modifying Objects During Iteration

javascriptjavascript
const data = { a: 1, b: 2, c: 3 };
 
// WRONG: modifying while iterating (unpredictable)
for (const key of Object.keys(data)) {
  if (data[key] < 2) delete data[key]; // Risky!
}
 
// CORRECT: create a new object with filter
const filtered = Object.fromEntries(
  Object.entries(data).filter(([, value]) => value >= 2)
);
console.log(filtered); // { b: 2, c: 3 }

Forgetting Object.keys Returns Strings

javascriptjavascript
const data = { 1: "one", 2: "two", 3: "three" };
 
Object.keys(data).forEach(key => {
  console.log(typeof key); // "string" (not number!)
  console.log(key === 1);  // false (string !== number)
  console.log(key === "1"); // true
});
Rune AI

Rune AI

Key Insights

  • Object.keys(): Returns an array of property names; ideal for counting, existence checks, and key iteration
  • Object.values(): Returns an array of property values; perfect for calculations and aggregations
  • Object.entries(): Returns [key, value] pairs; the most versatile for full transformation pipelines
  • Object.fromEntries(): Converts entries back to objects, completing the entries → transform → fromEntries workflow
  • Own enumerable only: All three methods skip inherited and non-enumerable properties, making them safer than for...in
RunePowered by Rune AI

Frequently Asked Questions

Do these methods include inherited properties?

No. All three methods (`Object.keys`, `Object.values`, `Object.entries`) return only the object's **own** enumerable properties. Properties inherited from the prototype chain are excluded. This is one reason they are preferred over `for...in` [loops](/tutorials/programming-languages/javascript/javascript-loops-tutorial-for-while-do-while), which do iterate over inherited properties.

Do they include Symbol properties?

No. These methods only return string-keyed properties. To get Symbol properties, use `Object.getOwnPropertySymbols(obj)`. To get all own properties (both strings and symbols), use `Reflect.ownKeys(obj)`.

What is the difference between Object.keys and for...in?

`Object.keys()` returns an array of own enumerable string keys. A `for...in` loop iterates over own AND inherited enumerable string keys. `Object.keys` is generally preferred because it avoids prototype chain surprises and returns an array you can chain with [map](/tutorials/programming-languages/javascript/how-to-use-the-javascript-array-map-method-today), [filter](/tutorials/programming-languages/javascript/javascript-array-filter-method-complete-tutorial), and reduce.

Can I use these on arrays?

Yes, since arrays are objects. `Object.keys([10, 20, 30])` returns `["0", "1", "2"]` (string indices), and `Object.values([10, 20, 30])` returns `[10, 20, 30]`. However, regular array methods are more appropriate for working with arrays.

Is the property order guaranteed?

Since ES2015, JavaScript engines maintain a deterministic property order: integer-like keys first (sorted numerically), then string keys in insertion order, then symbols in insertion order. `Object.keys`, `Object.values`, and `Object.entries` all follow this order.

Conclusion

Object.keys(), Object.values(), and Object.entries() bridge the gap between objects and arrays in JavaScript. They convert object data into arrays that you can iterate, transform, filter, and reduce using the full power of array methods. Combined with Object.fromEntries(), they form a complete pipeline for object data transformation. Mastering these four methods unlocks clean, functional patterns for working with any object-shaped data in your applications.