
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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>
- Related Articles
- How to get the length of an object in JavaScript?
- How to get Property Descriptors of an Object in JavaScript?
- How to get the first n values of an array in JavaScript?
- Converting a JavaScript object to an array of values - JavaScript
- How to edit values of an object inside an array in a class - JavaScript?
- How to get an object containing parameters of current URL in JavaScript?
- How to set default values when destructuring an object in JavaScript?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to modify key values in an object with JavaScript and remove the underscore?
- How to get the size of a json object in JavaScript?
- How to merge an array with an object where values are arrays - JavaScript
- How to get the numbers which can divide all values in an array - JavaScript
- Find n highest values in an object JavaScript
- Filter the properties of an object based on an array and get the filtered object JavaScript
- How to set JavaScript object values dynamically?
