How to Get a Key-Value Pair From a Map in JavaScript

2 days ago admin Javascript

In this lesson, we will see how to get a key-value pair from a map in JavaScript, sometimes we want to log each key-value pair of a given map in JavaScript so how can we do that? 


The forEach() method

To do that we can use the forEach() method that returns each key with the value.

                                                        
                                                                                                                        
const myMap = new Map([['name','john'],['age',22],['city','london']]);
myMap.forEach((value,key) => console.log(key,value));
//name john
//age 22
//city london

Popular Tutorials

Related Tutorials

How to Add Multiple Data Types for Vue js Props

In this lesson, we will see how to add multiple data types for vue js props, sometimes when working...


How to Convert an Object to a Map in JavaScript

In this lesson, we will see how to convert an object to a map in JavaScript, sometimes we want to co...


How to Create an Array From a Map in JavaScript

In this lesson, we will see how to create an array from a map in JavaScript, sometimes we want to co...


How to Get the Keys of a Map in JavaScript

In this lesson, we will see how to get the keys of a map in JavaScript, sometimes we want to get all...


How to Get the Values of a Map in JavaScript

In this lesson, we will see how to get the values of a map in JavaScript, sometimes we want to get a...


How to Get the Size of a Map in JavaScript

In this lesson, we will see how to get the size of a map in JavaScript, sometimes we want to get the...


How to Remove a Value from a Map in JavaScript

In this lesson, we will see how to remove a value from a map in JavaScript, sometimes we want to rem...


How to Check if a Map Has a Value in JavaScript

In this lesson, we will see how to check if a map has a value in JavaScript, sometimes we want to ch...


How to Get a Specific Value from a Map in JavaScript

In this lesson, we will see how to get a specific value from a map in JavaScript, sometimes we want...


How to Get the Index of a Value in an Array in JavaScript

In this lesson, we will see how to get the index of a value in an array in JavaScript, sometimes we...