
- 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 return an array whose elements are the enumerable property values of an object in JavaScript?
We can use some logical methods to get the values and also keys from an object, but those methods will not return the values as an array, which is very useful in many cases. Javascript has provided Object.values() method to get an array whose elements are the enumerable property values of an object.
syntax
Object.values(obj);
This method takes an object as an argument and returns an array whose elements are nothing but the property values of the object.
Example-1
In the following example, an object is sent through the method object.values() and the property values were displayed as an array.
<html> <body> <script> var obj = {"one":1,"two": 2,"three": 3}; document.write(Array.isArray(Object.values(obj))); document.write("</br>"); document.write(Object.values(obj)); </script> </body> </html>
Output
true 1,2,3
Example-2
In the following example, an object is sent through the method object.values() and the property values were displayed as an array. Array.isArray() is used to check whether the resulted object is an array or not.
<html> <body> <script> var object = {"name":"Elon","company":"Tesla","age":"47","property":"1 Billion dollars"}; document.write(Array.isArray(Object.values(object))); document.write("</br>"); document.write(Object.values(object)); </script> </body> </html>
Output
true Elon,Tesla,47,1 Billion dollars
- Related Articles
- JavaScript Count the number of unique elements in an array of objects by an object property?
- How to merge an array with an object where values are arrays - JavaScript
- How to return object from an array with highest key values along with name - JavaScript?
- How to edit values of an object inside an array in a class - JavaScript?
- Converting a JavaScript object to an array of values - JavaScript
- Return indexes of greatest values in an array in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- How to get the values of an object in JavaScript?
- Sorting an array of objects by property values - JavaScript
- What is the importance of "enumerable" attribute in defining a property in JavaScript object?
- How to delete a property of an object in JavaScript?
- How to get Property Descriptors of an Object in JavaScript?
- What are the properties of an array object in JavaScript?
- What are the methods of an array object in JavaScript?
- How to convert an object into an array in JavaScript?
