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.

  • Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
  • Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.

For instance, consider the following code:

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:

  • Object.values() is used for returning enumerable property values of a simple array.
  • Object.values() is used for returning enumerable property values of an array like object.
  • Object.values() is used for returning enumerable property values of an array like object with random key ordering.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


OUTPUT :

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

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


OUTPUT :

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

Code 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


OUTPUT :

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

Exceptions :

  • It causes a TypeError if the argument passed is not an object .
  • If an object is not passed as an argument to the method, then it persuades it and treats it as an object.

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.