To print a map in JavaScript, you can iterate over the map using the forEach method, a for-of loop, or the entries method to print each key-value pair.
We can print a map in JavaScript by iterating over the map using the forEach method and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.forEach
method to iterate over each key-value pair in the map. The forEach
method takes a callback function that defines the operation to be performed on each element.console.log
function.const myMap = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
// Print the map using forEach method
myMap.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
one: 1 two: 2 three: 3
We can print a map in JavaScript by iterating over the map using a for-of loop and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.entry
represents each key-value pair in the map as an array.console.log
function.const myMap = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
// Print the map using for-of loop
for (const [key, value] of myMap) {
console.log(`${key}: ${value}`);
}
one: 1 two: 2 three: 3
We can print a map in JavaScript by iterating over the map using the entries method and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.entries
method to get an iterator of the map's key-value pairs. The iterator returns each key-value pair as an array.entry
represents each key-value pair in the map as an array.console.log
function.const myMap = new Map([
['one', 1],
['two', 2],
['three', 3]
]);
// Print the map using entries method
for (const [key, value] of myMap.entries()) {
console.log(`${key}: ${value}`);
}
one: 1 two: 2 three: 3
In this tutorial, we learned How to Print a Map in JavaScript language with well detailed examples.