Object.values()
Returns an array of an object's own enumerable string-keyed property values.
Object.values(obj)- Added in JavaScript ES2017
Parameters
| Parameter | Type | Description |
|---|---|---|
$obj | Object | An object. Values that are not objects are coerced to objects first, except null and undefined. |
Return value
ArrayAn array containing the given object's own enumerable string-keyed property values, in the same order as a for...in loop (excluding prototype properties).
Errors
Throws a TypeError if obj is null or undefined, since neither can be coerced to an object.
Object.values() is the value-side counterpart to Object.keys(). It applies exactly the same filters — own properties only, enumerable only, string keys only — and returns the values in the same order the keys would come out in.
When to reach for it
Reach for it when the keys are labels you do not need in the calculation: summing amounts in a totals object, finding the maximum in a lookup table, feeding an object's contents into Array.prototype.filter() or .some(), or spreading values into Math.max(). If you find yourself writing Object.keys(o).map(k => o[k]), this is the method you wanted.
What trips people up
Getters are invoked. A value produced by an accessor property is computed at the moment you call Object.values(), so side effects in a getter fire here — easy to miss when logging an object for debugging.
The ordering rule bites here too, because there are no keys in the output to tell you the order was rearranged. Object.values({ 10: 'ten', 2: 'two' }) yields ['two', 'ten']. Values are also copied by reference: mutating a nested object you pulled out of the array mutates the original.
Example
When to use it
- Sum or average a scores-by-name object without caring about the names
- Find the largest entry in a lookup table with Math.max(...Object.values(o))
- Check whether any field in a form-state object is still empty
- Convert a keyed cache into a plain list for rendering
Frequently asked questions
What is the difference between Object.values() and Object.keys()?
Object.values() returns the property values while Object.keys() returns the property names; both apply the same own-and-enumerable filter and use the same ordering.
Does Object.values() work on arrays and strings?
Yes — arrays return their elements and strings return their characters, because indices are own enumerable properties; only null and undefined throw.
Why does Object.values() return the values in the wrong order?
Integer-like keys are always enumerated first in ascending numeric order, ahead of string keys in insertion order, so a numerically keyed object will not follow the order you typed it in.
More examples
Only own enumerable values
Only ["yes"] is printed: the inherited property and the non-enumerable property are both excluded.
const proto = { fromProto: "nope" };
const obj = Object.create(proto);
obj.visible = "yes";
Object.defineProperty(obj, "hidden", { value: "nope", enumerable: false });
console.log(Object.values(obj));Getters are evaluated
The getter runs once during the call, so the array holds the computed 12 and the counter proves the accessor was invoked.
let reads = 0;
const row = {
base: 10,
get withTax() {
reads++;
return this.base * 1.2;
},
};
console.log(Object.values(row));
console.log("getter calls:", reads);Values share references
Prints "Grace" — the array holds a reference to the same nested object, not a copy of it.
const state = { user: { name: "Ada" } };
const [firstValue] = Object.values(state);
firstValue.name = "Grace";
console.log(state.user.name);
Discussion