Lesson guide
Learning objectives
- Explain the main purpose of Object.keys, values, entries in JavaScript.
- Identify the syntax, APIs, or concepts introduced in this lesson.
- Use the examples to predict how JavaScript will behave before you run similar code.
- Connect this topic to nearby lessons in Data types.
Real-world context
This lesson matters when you need to recognize where Object.keys, values, entries fits into real JavaScript programs. Let’s step away from the individual data structures and talk about the iterations over them.
Key ideas
- Object.keys, values, entries is part of the Data types chapter, so it builds on the surrounding concepts rather than standing alone.
- Read each code example in two passes: first for the result, then for the rule that explains the result.
- When a section compares similar features, focus on the condition that makes you choose one feature over another.
Key terms
- Object.keys, values, entries
- Object
- keys
- values
- entries
Let’s step away from the individual data structures and talk about the iterations over them.
In the previous chapter we saw methods map.keys(), map.values(), map.entries().
These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too.
They are supported for:
MapSetArray
Plain objects also support similar methods, but the syntax is a bit different.
Object.keys, values, entries
For plain objects, the following methods are available:
- Object.keys(obj) – returns an array of keys.
- Object.values(obj) – returns an array of values.
- Object.entries(obj) – returns an array of
[key, value]pairs.
Please note the distinctions (compared to map for example):
| Map | Object | |
|---|---|---|
| Call syntax | map.keys() | Object.keys(obj), but not obj.keys() |
| Returns | iterable | “real” Array |
The first difference is that we have to call Object.keys(obj), and not obj.keys().
Why so? The main reason is flexibility. Remember, objects are a base of all complex structures in JavaScript. So we may have an object of our own like data that implements its own data.values() method. And we still can call Object.values(data) on it.
The second difference is that Object.* methods return “real” array objects, not just an iterable. That’s mainly for historical reasons.
For instance:
let user = { name: "John", age: 30 };
Object.keys(user) = ["name", "age"]Object.values(user) = ["John", 30]Object.entries(user) = [ ["name","John"], ["age",30] ]
Here’s an example of using Object.values to loop over property values:
let user = { name: "John", age: 30 }; // loop over values for (let value of Object.values(user)) { alert(value); // John, then 30 }
Object.keys/values/entries ignore symbolic properties
Just like a for..in loop, these methods ignore properties that use Symbol(...) as keys.
Usually that’s convenient. But if we want symbolic keys too, then there’s a separate method Object.getOwnPropertySymbols that returns an array of only symbolic keys. Also, there exist a method Reflect.ownKeys(obj) that returns all keys.
Transforming objects
Objects lack many methods that exist for arrays, e.g. map, filter and others.
If we’d like to apply them, then we can use Object.entries followed by Object.fromEntries:
- Use
Object.entries(obj)to get an array of key/value pairs fromobj. - Use array methods on that array, e.g.
map, to transform these key/value pairs. - Use
Object.fromEntries(array)on the resulting array to turn it back into an object.
For example, we have an object with prices, and would like to double them:
let prices = { banana: 1, orange: 2, meat: 4, }; let doublePrices = Object.fromEntries( // convert prices to array, map each key/value pair into another pair // and then fromEntries gives back the object Object.entries(prices).map(entry => [entry[0], entry[1] * 2]) ); alert(doublePrices.meat); // 8
It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
Common mistakes
- Skipping the small examples and then missing the exact rule that Object.keys, values, entries depends on.
- Copying code without changing one value at a time to see which part controls the result.
- Treating similar-looking syntax or APIs as interchangeable before checking their edge cases.
Summary
- Object.keys, values, entries gives you one more piece of the JavaScript mental model.
- The examples in this lesson are the quickest way to check whether the rule is clear.
- Revisit the key terms when you meet the same idea in later chapters.
Predict
Before running this object-iteration check, predict the five output lines. It uses keys, values, entries, fromEntries, and then compares them with Reflect.ownKeys.
Reveal explanation
The output is name,age, John|30, name:John,age:30, 4, and 3. Object.keys, Object.values, and Object.entries return real arrays for the string-keyed properties of user, in property order. They ignore the symbol key. The Object.entries(...).map(...).fromEntries(...) pattern converts an object to pairs, transforms the pairs, and builds a new object. Reflect.ownKeys(user) includes both string keys and the symbol key, so it counts three keys.
Try it
Replace Reflect.ownKeys(user) with Object.getOwnPropertySymbols(user), then predict the new length.
Practice
- Rewrite one example from this lesson without looking at the original, then run it and compare the result.
- Change one input, operator, method call, or option in a code sample and predict what will happen before running it.
- Explain Object.keys, values, entries in your own words as if you were reviewing it with another learner.
Keep learning
Continue with Destructuring assignment when you are ready for the next lesson.