The Object.values method generates an array that includes the enumerable property values belonging to the specified object. These values are arranged in the same sequence as they would appear when using a for...in loop.
Syntax:
Object.values(obj)
Parameter:
obj: This refers to the object from which the enumerable own property values will be retrieved.
Return value:
This approach yields an array containing the values of the enumerable properties that belong to a specified object.
Browser Support:
| Chrome | 54 |
|---|---|
Edge |
14 |
| Firefox | 47 |
| Opera | 41 |
Example 1
const object1 = {
a: 'Rahul',
b: 0,
c:false
};
console.log(Object.values(object1));
Output:
["Rahul", 0, false]
Example 2
const object1 = {
a: 'string',
b: 34,
c: true
};
const object2 = {
a: 'start',
b: 33,
c: false
};
console.log(Object.values(object1),
Object.values(object1));
Output:
["string", 34, true]
["string", 34, true]
Example 3
Object.values = function(object) {
var values = [];
for(var property in object) {
values.push(object[property]);
}
return values;
}
var foo = {a:1, b:2, c:3};
console.log(Object.values(foo));
Output:
[1, 2, 3]