The Object.values
method returns an
array of object values in the same order
as when iterating through a loop.
Syntax
let values = Object.values(object from which we derive values);
Example
Let's get values from the following object:
let obj = {'a': 1, 'b': 2, 'c': 3};
console.log(Object.values(obj));
The code execution result:
[1, 2, 3]
Example
And now put the values in the object in random order:
let obj = {2: 'b', 1: 'a', 4: 'd', 3: 'c'};
console.log(Object.values(obj));
After executing the code, we will see that the values are sorted in ascending order:
['a', 'b', 'c', 'd']
Example
Also, using the Object.values
method,
you can get the values (or elements) of arrays:
let arr = ['a', 'b', 'c', 'd'];
console.log(Object.values(arr));
The code execution result:
['a', 'b', 'c', 'd']
See also
-
the
Object.keys
method
that returns an array of object properties -
the
Object.assign
method
that copies properties and values of an object