How to get the values of an object in JavaScript?


There are some methods for finding the value of an object which is an object.values(), but when you are using this method the process will be lengthy.

We can find easily the value of an object with the help of the _.values() function it is a built-in method that belongs to underscore.js a javascript library that provides versatile functions.

_.value() this method requires no for loop to execute the values, it is a direct method to find the value of the object.

Syntax

_.values( object )

Parameters − This function accepts only one argument which is an object. An object is a like of array which has key-value pair but is not an array.

Example: 1

Following is an example to retrieve values from an object −

<!DOCTYPE html>
<html>
<body>
   <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>
</head>
<body>
   <script>
      var obj = {"name": 'Abcd kumar',age: 47, contact:9893444666, "Organization":'Spacex' }
      var res = JSON.stringify(_.values(obj)); // we are using stringify because we want to print the value in page not in console.
      document.write((res));
   </script>
</body>
</html>

Example 2

In this example we are printing the object value with the help of the object.value() function.

<!DOCTYPE html>
<html>
<body>
   <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>
</head>
<body>
   <script>
      var userDetails = {name: 'Aman Kumar',state:"Jharkhnad", work:'Technical Writer', company:"TutorialsPoint" };
      for(let value of Object.values(userDetails)){
         document.write(" " + value + " ");
      }
   </script>
</body>
</html>

Example 3

Let us see another example for this −

<!DOCTYPE html>
<html>
<head>
   <script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
</head>
<body>
   <script type="text/javascript">
      var details = _.values({
         Name: "Vivek",
         Address: "Noida Up",
         Mobile: "+91 9876452301",
         Email: "tutorialspoint@gmail.com",
      });
      
      //console.log(key);
      for(let value of details){
         document.write(" " + value + " ");
      }
   </script>
</body>
</html>

Example 4

Here is another example where we print the contents of an object in an array format −

<!DOCTYPE html>
<html>
<head>
   <script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"
></script>
</head>
<body>
   <script type="text/javascript">
      var details = _.values({
         Name: "Vivek",
         Address: "Noida",
         Mobile: "+91 9876452301",
         Email: "tutorialspoint@gmail.com",
      });
      
      var object = _.values({
         name: "Aman Kumar",
         Address: "Hyderabad",
         Mobile: "+91 8252240532",
      });
      
      //console.log(key);
      document.write("[");
      for (let value of details) {
            document.write(" " + value + " ");
         } // here we are using for loop and documet.write() to display the value in page.
         document.write("]");
         
         document.write("[");
         for (let x of object[2]) {
            document.write(x); // get the third item from the list returned as well
         }
         document.write("]");
   </script>
</body>
</html>

Updated on: 06-Dec-2022

417 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements