AppDividend
Latest Code Tutorials

Javascript Object Values Example | Object.values() Tutorial

0

Javascript Object Values Example | Object.values() Tutorial is today’s topic. The Object.values() method returns the array of the given object’s enumerable property values. The ordering of the properties is the same as that given by the object manually is the loop is applied to the properties. 

Javascript Object.values() takes an object as an argument of which an enumerable own property values are to be returned and returns the array containing all the enumerable property values of the given object.

Javascript Object Values Example

The syntax of Javascript Object.values() is following.

Object.values(obj)
An obj parameter whose enumerable own property values are to be returned.
The values() function returns an array containing the given object’s enumerable property values.

Let’s see the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
console.log(Object.values(student));

The output is following.

 

Javascript Object Values Example

So, it has returned an array of values of the passed object.

We can do the same thing using the for…in loop. See the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
let arr = [];
for (const prop in student) {
  arr.push(student[prop]);
}
console.log(arr);

The output is following.

 

Javascript For In Loop Tutorial

But, we do not have to use for…in loop whenever we have a ready-made method available to get the values of an object in the form of an array.

You can get an array of keys with Object.keys() and then use the map() method to get values. See the following example.

// app.js

let student = { name: 'KRUNAL', education: 'BE IT' };
let output = Object.keys(student).map(function(e) {
  return student[e]
})
console.log(output);

It will give us the same output.

Finally, Javascript Object Values Example | Object.values() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.