Extract values for mapping or filtering
Transforming records
JavaScript · Function
Object.values() returns an array of an object's own enumerable string-keyed property values. It excludes inherited and symbol-keyed properties.
Object.values() converts its argument to an object, gathers the values of its own enumerable properties whose keys are strings, and returns those values in property-enumeration order. It does not include inherited properties or symbol-keyed properties.
Object.values(obj)| Name | Type | Description |
|---|---|---|
obj | object | The value whose own enumerable string-keyed property values should be collected. |
An array containing the given object's own enumerable string-keyed property values.
const user = { name: "Ada", active: true, visits: 3 };
const values = Object.values(user);
console.log(values); // ["Ada", true, 3]Creates an array containing the object's own enumerable string-keyed values.
Transforming records
Validating state
Displaying data
It returns an array of an object's own enumerable string-keyed property values. Inherited properties and symbol-keyed properties are excluded.
Follow-up: How would you obtain both keys and values? Use Object.entries(), which returns paired key-value arrays.
The array follows the same property order used by a for...in loop for the object's own enumerable string-keyed properties. Unlike for...in, it does not continue through the prototype chain.
Follow-up: How can you include inherited enumerable properties? Iterate with for...in, while applying an ownership check if only selected inherited behavior is intended.
Its parameter is coerced to an object. null and undefined cannot be coerced and throw TypeError; strings expose their indexed characters, while other primitives return no values.
Follow-up: What should be used when property names are needed rather than values? Use Object.keys().
Object.keys()Object.entries()for...in