Sunday 21 October 2018

JavaScript: size: get number of pairs in map

Every map has a ‘size’ property, it returns the number of <key, value> pairs in the map.

HelloWorld.js
var countries = [["Russia", "Moscow"], ["Spain", "Madrid"]];

var countriesMap = new Map(countries);

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

for (var [country, capital] of countriesMap) {
  console.log(country + ' : ' + capital);
}

console.log("Total elements in countriesMap are : " + countriesMap.size);


Output

Russia : Moscow

Spain : Madrid
Bahrain : Manama
Cameroon : Yaounde
Norway : Oslo
India : New Delhi
Total elements in countriesMap are : 6



Previous                                                 Next                                                 Home

No comments:

Post a Comment