JavaScriptObjects and classes
Object. values()
Returns an array of an object’s own, enumerable property values.
- Returns
- An array of values.
- Changes the original
- No
Example
#JavaScript
const points = { html: 120, css: 90, js: 45 }
const total = Object.values(points).reduce((sum, p) => sum + p, 0)
console.log(Object.values(points))
console.log(total)Console
[120, 90, 45]
255
Definition and usage
#The Object.values() method works like Object.keys(), but returns the values instead of the names. The order matches the order of the keys.
It’s the shortest path to calculations on an object’s values, for example summing points stored as { html: 120, css: 90 }, or checking whether any value meets a condition.
The returned array is new, but the object values inside it aren’t copied. Changing an object inside the array also changes the object in the original.
Syntax
#Syntax
Object.values(obj)Parameters
#obj
object
The object whose values you want to get.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Combine
Object.values()withreduce()to total up the values. - Combine them with
some()to check whether any value meets a condition.
Related entries
#Spotted a mistake or missing an example? Write to us.