JavaScript

How to access Object Values in JavaScript

A JavaScript object comprises some keys and their corresponding values. In certain situations, you may only need to access the values stored in an Object. For instance, we have created an object that stores the player’s names on a leaderboard, and we need to write a JavaScript program for getting the names rather than the keys associated with them.

In such a scenario, “Object.values()” is a method that you can use in your JavaScript code. This method accesses the object values effortlessly. In this write-up, we will explain the procedure to access object values in JavaScript with the help of suitable examples. So, let’s start!

Object.values() Method in JavaScript

In JavaScript, Object.values() method is utilized for accessing the object values. This method accepts a JavaScript object as an argument and returns an array whose elements contain the enumerable property values of the specified Object. Also, Object.values() method retrieves the object values in the same order they are added during object declaration.

Syntax of Object.values() Method

Object.values(obj)

In the above-given syntax, “obj” represents the Object whose enumerable property values will be returned by the Object.values() method.

Now, check out the provided examples to know how to access object values in JavaScript.

Example 1: Access Single Object Values in JavaScript using Object.values() method

For the demonstration purpose, firstly, we will create a JavaScript object named “myObject” having three key: value pairs as follows:

constmyObject={  
  x: 'sharqa',  
  y: 0,  
  z: true  
};

Once “myObject” is declared, we can access its object values by using the “Object.values()” method:

console.log(Object.values(myObject));

You can see from the given output that the Object.values() method has returned the values of our “myObject” in the form of an array with the same order in which they are initially added:

Example 2: Access Multiple Object Values in JavaScript using Object.values() method

You can also access multiple object values by using the Object.values() method. For instance, we have created two objects, “obj1” and “obj2” and then added three “key: value” pairs to both of them:

constobj1={  
  x: 'Alex',  
  y: 25,  
  z: false
};  
constobj2={  
  a: 'Stepheny',  
  b: 23,  
  c: true  
};

Now, to access the values of the declared multiple objects, we will invoke “Object.values()” method two times while passing the “obj1” and “obj2,” respectively. Also, console.log() will display the values of these objects in the console window:

console.log(Object.values(obj1),Object.values(obj2));

Have a look at the below-given output:

How Object.values() method works in JavaScript

We have already mentioned that Object.values() accepts an object as an argument in JavaScript. After that, it declares an empty array such as “values“, as shown in the below-given example. Then, it iterates through the properties of the added Object, and for each property, it pushes its value to the “values” array. At the end of the iteration, the “values” array will be returned by the “Object.values()” method:

Object.values = function(myObject) {  
  var values = [];  
  for(varproperty in myObject) {  
    values.push(myObject[property]);  
  }  
  returnvalues;  
}

After defining the “Object.values()” method with the discussed functionality, we will create an “info” object and pass it as an argument to the “Object.values()” method:

var info = {x:11, y:22, z:33};

console.log(Object.values(info));

The above-given output signifies that we have successfully implemented the functionality of the Object.values() method in our JavaScript program.

Conclusion


The Object.values() method is used to access the object values in JavaScript. This JavaScript method takes an object as an argument and returns its property values in an array. It also iterates over each property to retrieve its value. This write-up discussed the procedure to access object values using the Object.values() method with the help of appropriate examples.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.