EN
TypeScript - add items to Map
0 points
In this article, we would like to show you how to add items to Map in TypeScript.
Quick solution:
xxxxxxxxxx
1
const myMap = new Map<string, string>();
2
3
myMap.set('key1', 'value1');
4
myMap.set('key2', 'value2');
5
myMap.set('key3', 'value3');
In this example, we use Map.prototype.set()
to add items to the Map.
xxxxxxxxxx
1
const myMap = new Map<string, string>();
2
3
myMap.set('key1', 'value1');
4
myMap.set('key2', 'value2');
5
myMap.set('key3', 'value3');
6
7
myMap.forEach((value: string, key: string) => {
8
console.log(key, value);
9
});
Output:
xxxxxxxxxx
1
key1 value1
2
key2 value2
3
key3 value3