Sunday 21 October 2018

JavaScript: Map.prototype.get(key): Return the value associated with key

Map.prototype.get(key)
This method returns the value associated with this key, or undefined, if there is not key present.

Example
  var keys = map.keys();
 
  for(var key of keys){
    console.log(key + " : " + map.get(key));
  }

Find the below working example.

HelloWorld.js
function printMap(map){
  console.log("***************************");
  console.log("Total elements in countriesMap are : " + map.size);
  
  var keys = map.keys();
  
  for(var key of keys){
    console.log(key + " : " + map.get(key));
  }
}

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