Object.values() and Object.entries()
You'll learn how to extract all the values from an object at once, or grab both keys and values together as pairs.
What You're Learning
In the previous lesson, you saw Object.keys() return an array of property names. JavaScript also provides Object.values() to get just the values, and Object.entries() to get both keys and values together as nested arrays.
Object.values() — Extracting All Values
Object.values() returns an array containing all enumerable property values from an object:
const product = {
name: 'Laptop',
price: 999,
inStock: true
};
const values = Object.values(product);
console.log(values); // ['Laptop', 999, true]
Notice you get just the values, with no keys attached. The order matches the order of properties in the object.
Object.entries() — Getting Key-Value Pairs
Object.entries() returns an array of [key, value] pairs:
const product = {
name: 'Laptop',
price: 999,
inStock: true
};
const entries = Object.entries(product);
console.log(entries);
// [['name', 'Laptop'], ['price', 999], ['inStock', true]]
Each item in the array is itself a two-element array: the first element is the key (as a string), the second is the value.
When to Use Each
Use Object.values() when you only care about the data, not the property names. Use Object.entries() when you need to process both keys and values together — it's especially useful for looping or transforming objects.
const scores = { alice: 95, bob: 87, charlie: 92 };
// Just the scores
console.log(Object.values(scores)); // [95, 87, 92]
// Both names and scores
console.log(Object.entries(scores));
// [['alice', 95], ['bob', 87], ['charlie', 92]]
Key Takeaway: Use Object.values() to extract just values and Object.entries() to get [key, value] pairs from an object.