Javatpoint Logo
Javatpoint Logo

TypeScript Map

TypeScript map is a new data structure added in ES6 version of JavaScript. It allows us to store data in a key-value pair and remembers the original insertion order of the keys similar to other programming languages. In TypeScript map, we can use any value either as a key or as a value.

Create Map

We can create a map as below.

Map methods

The TypeScript map methods are listed below.

SN Methods Descriptions
1. map.set(key, value) It is used to add entries in the map.
2. map.get(key) It is used to retrieve entries from the map. It returns undefined if the key does not exist in the map.
3. map.has(key) It returns true if the key is present in the map. Otherwise, it returns false.
4. map.delete(key) It is used to remove the entries by the key.
5. map.size() It is used to returns the size of the map.
6. map.clear() It removes everything from the map.

Example

We can understand the map methods from the following example.

Output:

When we execute the above code snippet, it returns the following output.

TypeScript map

Iterating Map Data

We can iterate over map keys or values or entries by using 'for...of' loop. The following example helps to understand it more clearly.

Example

Output:

TypeScript map
Next TopicTypeScript Set




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA