Understanding Object.values()

Understanding Object.values()

A quick guide to understanding `Object.values()`

๐Ÿ‘‰ 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()

  1. It returns the elements in the same order as that provided by a for...in loop.
  2. The only difference is that a for...in loop enumerates properties in the prototype chain as well.
  3. The result of Object.values() is an array of all the values so you can map all the array methods to it.
  4. 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:

ย