The Wayback Machine - https://web.archive.org/web/20211027211312/https://www.geeksforgeeks.org/object-values-javascript/amp/

Object.values( ) In JavaScript

Object and Object Constructors in JavaScript? 
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations. 
 

For instance, consider the following code:
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

function Automobile(color) {
  this.color=color;
}

var vehicle1 = new Automobile ("red");

The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”. 
When new Automobile() is called, JavaScript does two things: 
 

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.values() Method 
Object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually is a loop is applied to the properties. 
Object.values() takes the object as an argument of which the enumerable own property values are to be returned and returns an array containing all the enumerable property values of the given object.
Applications: 
 



Syntax: 
 

Object.values(obj)

Parameters Used: 
 

  1. obj : It is the object whose enumerable property values are to be returned.

Return Value: 
Object.values() returns an array containing all the enumerable property values of the given object. 
 

Examples of the above function are provided below.
Examples: 
 

Input : var check = ['x', 'y', 'z'];
        console.log(Object.values(check));
Output : Array ["x", "y", "z"]

Explanation: In this example, an array “check” has three property values [‘x’, ‘y’, ‘z’] and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 
 

Input : var object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
        console.log(Object.values(object));
Output : Array ["23", "geeksforgeeks", "true"]

Explanation: In this example, an array like object “check” has three property values { 0: ’23’, 1: ‘geeksforgeeks’, 2: ‘true’ } and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 
 

Input : var object = { 70: 'x', 21: 'y', 35: 'z' };
        console.log(Object.values(object));
Output : Array ["y", "z", "x"]

Explanation: In this example, an array like object “check” has three property values { 70: ‘x’, 21: ‘y’, 35: ‘z’ } in random ordering and the object.values() method returns the enumerable property values of this array in the ascending order of the value of indices.
Codes for the above function are provided below.
Code 1:
 




<script>
 
// Returning enumerable property values of a simple array
var check = ['x', 'y', 'z'];
console.log(Object.values(check));
 
</script>

OUTPUT : 
 



Array ["x", "y", "z"]

Code 2:
 




<script>
 
// Returning enumerable property values
// of an array like object.
var object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
console.log(Object.values(object));
 
</script>

OUTPUT : 
 

Array ["23", "geeksforgeeks", "true"]

Code 3:
 




<script>
 
// Returning enumerable property values
// of an array like object.
var object = { 70: 'x', 21: 'y', 35: 'z' };
console.log(Object.values(object));
 
</script>

OUTPUT : 
 

 Array ["y", "z", "x"]

Exceptions : 
 

Supported Browser:

Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
 




Article Tags :