Javascript - Map Iteration and Methods

Introduction

You can create a map via an iterable over key/value pairs:

let myMap = new Map([ 
    [ 1, 'XML' ], 
    [ 2, 'Screen' ], 
    [ 3, 'Keyboard' ] 
]); 

Also, maps can be iterated over using for...of or forEach():

for (let [key, value] of myMap) { 
   console.log(key + " = " + value); 
} 
// 1 = XML 
// 2 = Screen 
// 3 = Keyboard 

Maps also support keys(), values(), and entries() methods:

for (let key of myMap.keys()) { 
   console.log(key); 
} 
// 1 
// 2 
// 3 

for (let value of myMap.values()) { 
   console.log(value); 
} 
// XML 
// Screen 
// Keyboard 

for (let [key, value] of myMap.entries()) { 
   console.log(key + " = " + value); 
} 
// 1 = XML 
// 2 = Screen 
// 3 = Keyboard 

Related Topic