EN
TypeScript - get Map item value by key
0
points
In this article, we would like to show you how to get Map item value by key in TypeScript.
Quick solution:
const myMap = new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3'],
]);
const result = myMap.get('key1'); // value1
Practical example
In this example, we use get() method to get values from the Map by item keys.
const myMap = new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3'],
]);
console.log(myMap.get('key1'));
console.log(myMap.get('key2'));
console.log(myMap.get('key3'));
Output:
value1
value2
value3