๐ The Object.values()
method returns an array of all the property values of an object.
๐ Syntax
Object.values(object);
๐ Here are some of the major points you have to remember while using Object.values()
- It returns the elements in the same order as that provided by a
for...in
loop. - The only difference is that a
for...in
loop enumerates properties in the prototype chain as well. - The result of
Object.values()
is an array of all the values so you can map all the array methods to it. - This method became part of JavaScript in ES2017.
๐ Example:
const user = {
name: 'Jhon doe',
age: 34,
isActive: false
};
// Using `for...in`
for (let key in user) {
console.log(user[key]);
}
// Using `Object.values()`
Object.values(user).forEach(value => console.log(value));
๐ References:
๐ Follow me:
ย