Object Entries and Values
This lesson explains Object Entries and Values with clear examples, use cases, and best practices so you can use modern JavaScript confidently in real projects.
ES6+ Object Entries and Values Overview
Object.entries() and
Object.values() let you extract data from objects as
arrays. They make it easier to loop, transform, filter, and convert
objects in modern JavaScript.
These methods are commonly used with array methods such as
map(), filter(), and
reduce(), and they pair well with
Object.fromEntries() when rebuilding objects.
| Feature | Description |
Object.entries() | Returns an array of [key, value] pairs |
Object.values() | Returns an array of property values |
| Introduced In | ES2017 |
| Related Method | Object.keys() returns property names |
| Reverse Helper | Object.fromEntries() |
| Common Uses | Looping, filtering, transforming, Map conversion |
Basic Object.entries and Object.values Example
const user = {
id: 1,
name: "Alex",
role: "admin",
active: true
};
console.log(
Object.entries(user)
);
console.log(
Object.values(user)
);
console.log(
Object.keys(user)
);
Object.entries() gives key-value pairs,
Object.values() gives only values, and
Object.keys() gives only property names.
How Object Entries and Values Work
Both methods convert object data into arrays so you can use familiar
array operations instead of manual loops with
for...in.
| Step | Description | Example |
| Start with Object | Have a plain JavaScript object. | const settings = { ... } |
| Extract Data | Call entries or values method. | Object.entries(settings) |
| Get Array | Receive iterable array output. | [["theme", "dark"]] |
| Transform | Use map, filter, or reduce. | .map(...) |
| Rebuild if Needed | Convert back with fromEntries. | Object.fromEntries(...) |
Object.keys, values, and entries Comparison
| Method | Returns | Example Output |
Object.keys() | Array of property names | ["id", "name", "role"] |
Object.values() | Array of property values | [1, "Alex", "admin"] |
Object.entries() | Array of key-value pairs | [["id", 1], ["name", "Alex"]] |
Looping with Object.entries
const product = {
title: "Keyboard",
price: 79,
stock: 12
};
for (const [key, value] of
Object.entries(product)) {
console.log(
`${key}: ${value}`
);
}
Object.entries() works naturally with
for...of and destructuring to iterate over object
properties cleanly.
Filter Object Properties
const scores = {
math: 88,
science: 92,
history: 74,
art: 95
};
const highScores =
Object.fromEntries(
Object.entries(scores).filter(
([, score]) => score >= 90
)
);
console.log(highScores);
A common pattern is to convert an object to entries, filter the
array, and rebuild the object with
Object.fromEntries().
Working with Object.values
const inventory = {
keyboard: 15,
mouse: 40,
monitor: 8
};
const totalItems =
Object.values(inventory).reduce(
(sum, count) => sum + count,
0
);
const maxStock = Math.max(
...Object.values(inventory)
);
console.log(totalItems);
console.log(maxStock);
Object.values() is useful when you only care about
the values, such as totals, averages, or max/min calculations.
Convert Object to Map
const settings = {
theme: "dark",
language: "en",
notifications: true
};
const settingsMap =
new Map(
Object.entries(settings)
);
console.log(
settingsMap.get("theme")
);
for (const [key, value] of settingsMap) {
console.log(key, value);
}
Object.entries() is the standard way to convert a
plain object into a Map when Map methods are needed.
Object.fromEntries Example
const pairs = [
["id", 10],
["name", "Riya"],
["role", "editor"]
];
const user =
Object.fromEntries(pairs);
console.log(user);
const swapped =
Object.fromEntries(
Object.entries(user).map(
([key, value]) => [
value,
key
]
)
);
console.log(swapped);
Object.fromEntries() rebuilds an object from an array
of entries and is the reverse operation of
Object.entries().
Common Use Cases
- Looping over object properties with
for...of. - Filtering or transforming object data.
- Calculating totals from object values.
- Converting objects to Maps and back.
- Building query strings or API payloads.
- Sorting or comparing object entries.
- Cloning and modifying objects immutably.
Object Entries and Values Best Practices
- Use
Object.entries() when you need both keys and values. - Use
Object.values() when only values matter. - Combine with
Object.fromEntries() for object transformations. - Prefer these methods over manual
for...in loops in modern code. - Remember they only include own enumerable properties.
- Use destructuring in loops for cleaner syntax.
- Keep transformations readable with small helper steps.
Common Mistakes
- Expecting inherited properties to be included.
- Using
Object.values() when keys are also needed. - Forgetting to rebuild the object after filtering entries.
- Assuming property order is guaranteed in all old environments.
- Confusing
Object.keys() with Object.entries(). - Mutating the original object when an immutable update was intended.
- Using these methods on
null or undefined.
Key Takeaways
Object.entries() returns arrays of [key, value] pairs. Object.values() returns an array of property values. - They make object iteration and transformation much easier.
- Use
Object.fromEntries() to convert entries back into objects. - These methods are essential tools for modern JavaScript data handling.
Pro Tip
For object filtering or transformation, remember the pattern:
Object.fromEntries(Object.entries(obj).filter(...)).