TypeScript Map is a collection that stores key-value pairs, where keys and values can be of any type. It maintains the insertion order of keys and provides methods to add, retrieve, check, remove, and clear entries, ensuring efficient management of key-value data.
Creating a Map
A map can be created as:
let myMap = new Map();
Map Methods
Map Methods | Description |
---|
map.set(key,value) | Used to add entries in the map. |
---|
map.get(key) | Used to access entries from the map. Returns undefined if the key does not exist in the map. |
---|
map.has(key) | Returns true if the key is present in the map. |
---|
map.delete(key) | Used to remove entries by key in the map. |
---|
map.size() | Used to return the size of the map. |
---|
map.clear() | Removes everything from the map. |
---|
Iterating Map Data
Iterating over the key-value pairs in a TypeScript Map can be done using various methods. The forEach
the method is commonly used for this purpose.
Example 1: Using the forEach Method
In this example, we are using the forEach method to Iterate over map data:
JavaScript
let myMap = new Map<string, number>();
myMap.set('one', 1);
myMap.set('two', 2);
myMap.set('three', 3);
myMap.forEach((value, key) => {
console.log(`Key: ${key}, Value: ${value}`);
});
Output:
Key: one, Value: 1
Key: two, Value: 2
Key: three, Value: 3
Example 2: Common Operations on a TypeScript Map
In this example, KeyType is string, and ValueType is number. The methods demonstrate common operations on a TypeScript Map.
JavaScript
let myMap = new Map<string, number>();
myMap.set('one', 1);
myMap.set('two', 2);
console.log(myMap.get('one')); // Output: 1
console.log(myMap.has('two')); // Output: true
myMap.delete('two');
console.log(myMap.get('two')); // Output: undefined
console.log(`Size: ${myMap.size}`); // Output: Size: 1
myMap.clear();
console.log(`Size after clear: ${myMap.size}`); // Output: Size after clear: 0
myMap.set('one', 1);
myMap.forEach((value, key) => {
console.log(`Key: ${key}, Value: ${value}`);
});
Output:
1
true
undefined
Size: 1
Size after clear: 0
Key: one, Value: 1
Similar Reads
TypeScript Mapped Types Mapped types in TypeScript allow you to create new types by transforming the properties of existing types.They enable modifications like making properties optional, read-only, or altering their types.Mapped types help reduce code duplication and enhance type safety by automating type transformations
3 min read
Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
3 min read
Type Manipulation in TypeScript TypeScript offers strong tools for types manipulation and transformation, these tools allow programmers to create new types by composing, intersecting, unionizing, mapping and conditioning existing ones, in this article we will investigate some of the advanced type-manipulation features in TypeScrip
3 min read
Typescript Generic Type Array A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri
1 min read
Typescript Keyof Type Operator The TypeScript keyof operator is used to get a union of all keys in an object type. Itâs useful when you want to work with the property names of an object in a type-safe way, ensuring only valid keys are used.We can use it to define generic functions that work with any object type, without knowing t
3 min read