Sunday, 21 October 2018

JavaScript: Map.prototype.forEach(callbackFn[, thisArg])

This method calls the call back function for every <key, value> pair.

 map.forEach(function(value, key) {
    console.log(key + ' = ' + value);
  });

Find the below working application.

HelloWorld.js
function printMap(map){
  console.log("***************************");
  console.log("Total elements in countriesMap are : " + map.size);
  
 map.forEach(function(value, key) {
    console.log(key + ' = ' + value);
  });
  console.log("***************************");
}

var countriesMap = new Map();

countriesMap.set("Bahrain", "Manama");
countriesMap.set("Cameroon", "Yaounde");
countriesMap.set("Norway", "Oslo");
countriesMap.set("India", "New Delhi");
countriesMap.set("Russia", "Moscow");
countriesMap.set("Spain", "Madrid");

printMap(countriesMap);


Output
***************************
Total elements in countriesMap are : 6
Bahrain = Manama
Cameroon = Yaounde
Norway = Oslo
India = New Delhi
Russia = Moscow
Spain = Madrid
***************************



Previous                                                 Next                                                 Home

No comments:

Post a Comment