Object.entries() Method JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Syntax of Object.entries() in JavaScript

The syntax for Object.entries() JavaScript is as follows:

Parameters of Object.entries() in JavaScript

The parameters of object entry javascript are as follows:

  • Parameter - Obj

This is the object whose property is returned as [key,value] pair.

Return Value of Object.entries() in JavaScript

The return value of object entry javascript:

  • Return value - an array of object

The object entries javascript method returns an array of objects as the return value. Each element of array is form of a [key,value] pair.

Exceptions of Object.entries() in JavaScript

Following are the exceptions of the object entry javascript method:

  • This method may lead to an error named TypeError. This error occurs when the argument that is passed is not an object.
  • This method also causes an error named RangeError. This error occurs if the argument that we passed in the argument is not found in the range of that particular property pair that is [key, value].

Example

Now let us see an example of the object entries javascript method and understand this method.

Output:

Explanation :

In the above example, we can see how the method object entry javascript is used to get the details like name, age of a person in the output. In this code, there are some own enumerable string-keyed properties like nameFirst, nameLast, and age of the object named person. These properties of a person are described inside the object person. Then the object.entry method is used to print all these properties of person as the output. The identity_number is not a string-key property of that particular object person, that is why it is not shown in the resulting output.

What is Object.entries() in JavaScript?

As we know, classes and objects are a very essential part of object-oriented programming. But in JavaScript, there are no traditional classes as all other languages have. In JavaScript, there are objects and constructors whose work is to perform the same action that objects and constructors do in other programming languages. Let us have a brief knowledge of the constructors.

  • The constructors are functions in JavaScript that are used for defining an object "type". And this constructor can be used as many times as we want in the program. We do not need to define the object every time we use it. Constructors in JavaScript are used with the new keyword. There are two types of constructors in JavaScript. The first one is "build-in constructor" and the second is "custom constructor". Built-in constructors are arrays and objects while the custom constructor is the properties and the methods for the objects.

The object.entry() is a method in JavaScript that is used for returning an array that consists of enumerable property [key, value] pairs of the object. These values are passed as the parameter for the object. The method entry() is a static method. And this method is called by the class name. The order of the properties of the object will remain the same that is provided by the loop of the object by the user. If different ordering is needed, then we need to sort the array first. This sorting will be done in the following way-

Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));

More Examples

Now let us see various examples of object entry javascript methods to understand in a better way. All the examples are explained with output and explanation where ever necessary.

Example 1

In this example, we will use the object entry javascript method to provide the particular properties of an object using an array index.

Output:

Explanation :

In the above example, an object named object_1 is created. In it, various properties and their values are entered such as name and age. After that, the console.log is used to print the output. In console.log, the object entry javascript method is used to print the desired output, and the index of property is described in the object.entry() method that needs to be printed.

Supported Browsers

The object entry javascript method supports a vast range of modern browsers that we generally use today. Following are the names of such browsers and their supported versions given below using a table.

Browser nameversions
Google chrome54 and above
Edge Browser14 and above
Firefox47 and above
Opera browser41 and above
Safari10.1 and above

Conclusion

  • In 2017, the ES8 introduced two new methods for javascript that are Object.values() and Object.entries() method.
  • The object entry javascript method is used for returning the properties of an object that is owned by the object. This value is the key-value pair of that particular property.
  • The object entry javascript method can be written in the following manner: Object.entries(obj).
  • The parameter of the object.entry() method is obj,, which is the object whose property this method will return.
  • The object entry javascript method returns an array of objects as the return value. This return value returns the enumerable property of the object such as [key, value] pairs.
  • The object.entry() method gives two types of error TypeError and RangeError.
  • Various browsers that support the object.entry() methods in JavaScript are GoogleChrome, Edge browser, Firefox, Opera Browser, and Safari.