Functions - JavaScript entries()
Posted by Superadmin on May 07 2023 07:52:44

JavaScript entries()

By Priya PedamkarPriya Pedamkar
  

JavaScript entries()

Introduction to JavaScript entries()

JavaScript is one of the rare object-oriented programming languages which does not include the conventional classes like seen in other languages but has objects and constructors working in a similar way performing a similar set of operations. The constructors here are common Javascript methods and used along with the keyword called “new”. There are two kinds of constructors in Javascript first one is called the built-in constructors (Ex: objects and arrays) and the second one is called the custom constructors (Ex: They describe certain properties and functions for specific objects). Constructors are useful when we have to create an object “type” which is helpful as we can use it any number of times without having to reformulate the object each time and this can be performed using an Object Constructor function.

Conventionally we give the names of constructors in capital to identify them as compared with other methods.

Consider the below Example:

function Animal(name) {
this.name=name;
}
var animal1 = new Animal ("cat");

The function here is “Animal” which is an object constructor whose methods and properties i.e the “name” in this case is declared inside it by giving it a prefix of “this”. Next during object declaration, we use the “new” keyword and the same objects are then made to instants.

When the function “Animal” is called, Javascript does the below two things:

Syntax:

array.entries()

Examples to Implement of JavaScript entries()

Below are the examples of JavaScript entries():

Example #1

Code:

// Declaration of the array
const arr = ['one', 'two', 'three'];
for (const [i, e] of arr.entries())
console.log(i, e);

Output:

JavaScript Entries() Example 1

In this example, we are showing basic use of the array entries function. Here i stands for index and e for element. They represent the key-value pairs for the entries function and same can be seen in the output.

Example #2

Code:

//Declaration of variables
var arr = ['one', 'two', 'three'];
var iterator = arr.entries();
for (let it of iterator) {
console.log(it);
}

Output:

JavaScript Entries() Example 2

In this example we are displaying a simple case on how to use array entries. For this we are first declaring the array who’s name is “arr”. We then call the array entries() function and then create an instance of iterator. We use “for loop” to print the return values of the entries() function.

Example #3

Code:

const array= [ 'example', 'for', 'array'];
var iter = array.entries();
// expected output: Array [ 0, 'example' ]
console.log(iter.next().value);
// expected output: Array [ 1, 'for' ]
console.log(iter.next().value);
// expected output: Array [ 2, 'array' ]
console.log(iter.next().value);

Output:

JavaScript Entries() Example 3

In this example, we are displaying how to print the output which is pointing to each index belonging to their respective key-value pairs of the array.

Object.entries() Method in JavaScript

This method object.entries() returns an array i.terator object which consists of key-value pairs which are basically the input parameters to this function. The order in which the properties are here is the same as that which is taken from the looping of the property values of the given object manually.

Syntax:

Object.entries(obj)

There is also another function called object.values() in Javascript and this returns the array values which are found on the object. Let us consider the below example to understand their difference.

Example #4

Code:

var obj = { 0: '55', 1: 'string', 2: 'false' };
console.log(Object.values(obj));
console.log(Object.entries(obj));

Output:

String Example 4

In this example, we have created an object with its respective key-value pairs called “obj”.

As we can see in the output first line represents the output of the object.values() function which returns the output in the form of key-value pairs of the object whereas the object.entries() function returns only the values and not their key values. Object.entries() are used to list down object-related properties and to list down all their key-value pairs.

Let us take a few examples to understand this function better.

Example #5

Code:

// Creation of the object constructor
// and assignment of values to it
const arr = { 0: 'john', 1: 'adam', 2: 'bill' };
// Display the key-value pairs in
// the output using object.entries() method
console.log(Object.entries(arr)[1]);

Output:

constructor Example 5

In this example we create an object called “arr” and we are specifying 3 properties here. Then we use our object.entries() method to return only the first property i.e the key-value pair of the object.

Example #6

Code:

// Creation of object and
// assignment of name values
const arr1 = { 10: 'Tim', 200: 'Fred', 35: 'Morris' };
// To display enumerable key-value pairs
// of the declared object using object.entries() method
console.log(Object.entries(arr1));

Output:

JavaScript Entries() Example 6

In this example, we are creating an object named “arr1” and giving 3 properties having different names as input. Then by using the object.entries() method we are using it to return key-value pairs of the entire object.

Example #7

Code:

// Creation of object with properties a and b
const object = { a: 'bar', b: 42 };
// Creation of map constructor
const mapping = new Map(Object.entries(object));
// To display the map converted elements
console.log(mapping);

Output:

Return key-value Example 7

In this example, we will look at how we can convert the object into a map. We create an object having 2 properties first. Then we create a new map constructor with the name “mapping” which accepts entries that are iterable. Hence by using object.entries() function we can easily convert an object to a map.

There are a few exceptions which are caused while using this function and which we need to aware of and they are as follows:

Conclusion

Hence in this article, we have gone through 2 of the major entries function used in Javascript: one being the array.entries() and another is the object.entries(). Object.entries() method is used for returning back an array of the given object in key-value pairs whereas array.entries() function is used to fetch a new array having the key-value pairs for each of the array indexes.