map get javascript code example

Example 1: node get value from map

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");

console.log(myMap.has("key1")); // true
console.log(myMap.has("key2")); // true

console.log(myMap.get("key1")); // value1
console.log(myMap.get("key2")); // value2

// NOTE: DO NOT try to access map values using [].
// myMap["key1"] = "value1";

Example 2: map in javascript

let myMap = new Map()

let keyString = 'a string'
let keyObj    = {}
// setting the values
myMap.set(keyString, "value associated with 'a string'")
myMap.set(keyObj, 'value associated with keyObj')
myMap.set(keyFunc, 'value associated with keyFunc')
myMap.size              // 3
// getting the values
myMap.get(keyString)    // "value associated with 'a string'"
myMap.get(keyObj)       // "value associated with keyObj"
myMap.get(keyFunc)      // "value associated with keyFunc"