Open In App

JavaScript Object entries() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

JavaScript 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.

Syntax:

Object.entries(obj);

Parameters:

  • obj: It is the object whose enumerable 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.

Example 1: 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.

javascript




// 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]);


Output

[ '1', 'billy' ]

Example 2: 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. 

javascript




// 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));


Output

[ [ '10', 'adam' ], [ '35', 'chris' ], [ '200', 'billy' ] ]

Applications

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

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.

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers:

  • Chrome 54 and above
  • Edge 14 and above
  • Firefox 47 and above
  • Opera 41 and above
  • Safari 10.1 and above

Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads