JavaScript Map entries() Method Tutorial

In this section, we will learn what the entries() method is and how to use it in JavaScript.

What is JavaScript Map entries() Method?

Each key and value that we put on a map is considered as one entry. So via the `entries()` method we can get an iterator object that contains the entire entries of the target map.

Note: In the Iterator section we’ve explained in greater details how to work with iterator, but in short, one way to get the contents of such an object is to use the `for of` loop. Basically, the `for of` will loop through all the content of the target iterator object and return all the entries one at a time.

JavaScript Map entries() Method Syntax:

entries()

entries() Function Parameters:

The entries() method does not take an argument.

entries() Function Return Value:

The return value of this method is an iterator object that contains each entry [key,value] of the target map object.

Example: using Map entries() method in JavaScript

const map = new Map();
map.set("John","222-22222222");
map.set(22 , 2000);
map.set(true, false); 
map.set("Jack", "333-3234232");

for (const entry of map.entries()){
  console.log("***");
  console.log(`The entry is: ${entry}`);
  console.log(`The key of this entry is: ${entry[0]} and the value is: ${entry[1]}`);
}

Output:

***

The entry is: John,222-22222222

The key of this entry is: John and the value is: 222-22222222

***

The entry is: 22,2000

The key of this entry is:22 and the value is: 2000

***

The entry is: true,false

The key of this entry is: true and the value is: false

***

The entry is: Jack,333-3234232

The key of this entry is: Jack and the value is: 333-3234232

Note: each of the entry that we get back from the `entries()` method is essentially an array with only two elements. The first element is the key and the second element is the value.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies