Object.entries() In JavaScript - GeeksforGeeks

22 Dec, 2021
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:
It creates a fresh new object(instance) Automobile() and assigns it to a variable.It sets the constructor property i.e “color” of the object to Automobile.
It creates a fresh new object(instance) Automobile() and assigns it to a variable.
It sets the constructor property i.e “color” of the object to Automobile.
Object.entries() MethodObject.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:
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:
<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>
OUTPUT :
Array ["1", "billy"]
Code 2:
<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>
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 .
Supported Browsers:
Chrome 54 and above
Edge 14 and above
Firefox 47 and above
Opera 41 and above
Safari 10.1 and above
Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
Pallavi Yadav
ysachin2314
javascript-functions
javascript-object
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
How to read a local text file using JavaScript?
Node.js | fs.writeFileSync() Method