Languages
[Edit]
EN

TypeScript - add items to Map

0 points
Created by:
Selina-Miranda
737

In this article, we would like to show you how to add items to Map in TypeScript.

Quick solution:

const myMap = new Map<string, string>();

myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');

 

Practical example

In this example, we use Map.prototype.set() to add items to the Map.

const myMap = new Map<string, string>();

myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');

myMap.forEach((value: string, key: string) => {
  console.log(key, value);
});

Output:

key1 value1
key2 value2
key3 value3
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join