

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- 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 the values 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?
- 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
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Sorting an array of objects by property values - JavaScript
- How to return object from an array with highest key values along with name - JavaScript?
- How to delete a property of an object in JavaScript?
- How to get Property Descriptors of an Object in JavaScript?
- How to convert an object into an array in JavaScript?