Object.entries() 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.entries() Method
Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Difference between Object.entries() and Object.values() method

Object.entries() method in JavaScript returns an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter whereas Object.values() method in JavaScript returns an array whose elements are the enumerable property values found on the object. Follow the example below for better understanding of the differences between these two functions.

Input: var object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
       console.log(Object.values(object));
       console.log(Object.entries(object));

Output: Array ["23", "geeksforgeeks", "true"]
        Array [["0", "23"],  ["1", "geeksforgeeks"],["2", "true"]]

Explanation: In the above example an object has been created with three [key, value] pairs and the object.entries() method returns the [key, value] pairs of the object and object.values() method returns the values found on the object.

Applications:

  • Object.entries() is used for listing properties related to an object.
  • Object.entries() is used for listing all the [key,value] pairs of an object.

Syntax:

Object.entries(obj)

Parameters Used:

  • obj : It is the object whose enumerable own property [key, value] pairs are to be returned.
  • Return Value:

  • Object.entries() returns an array consisting of enumerable property [key, value] pairs of the object passed.

Examples of the above function are provided below.

Examples:

Input : const obj = { 0: 'adam', 1: 'billy', 2: 'chris' };
        console.log(Object.entries(obj)[1]);

Output : Array ["1", "billy"]

Explanation: In this example, an object “obj” has been created with three property[key, value] pairs and the Object.entries() method is used to return the first property [key, value] pair of the object.

Input : const obj = { 10: 'adam', 200: 'billy', 35: 'chris' };
        console.log(Object.entries(obj)); 

Output : Array [ ["10", "adam"], ["35", "chris"], ["200", "billy"]]

Explanation: In this example, an object “obj” has been created with three property[key, value] pairs and the Object.entries() method is used to return all the property [key, value] pairs of the object.

Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating an object constructor
// and assigning values to it 
const obj = { 0: 'adam', 1: 'billy', 2: 'chris' };
  
// Displaying the enumerable property [key, value] 
// pairs of the object using object.entries() method 
console.log(Object.entries(obj)[1]);
</script>

chevron_right


OUTPUT :

Array ["1", "billy"]

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating an object constructor and 
// assigning values to it 
const obj = { 10: 'adam', 200: 'billy', 35: 'chris' };
  
// Displaying the enumerable property [key, value] 
// pairs of the object using object.entries() method 
console.log(Object.entries(obj)); 
</script>

chevron_right


OUTPUT :

Array [["10", "adam"], ["35", "chris"],["200", "billy"]]

Exceptions :

  • It causes a TypeError if the argument passed is not an object .
  • It causes a RangeError if the key passed in the argument is not in the range of the property[key, value] pair .

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



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.



Improved By : Pallavi Yadav