How to Iterate over Map Elements in TypeScript ?
Last Updated :
01 Aug, 2024
In TypeScript, iterating over the Map elements means accessing and traversing over the key-value pairs of the Map Data Structure. The Map is nothing but the iterative interface in TypeScript. We can iterate over the Map elements in TypeScript using various approaches that include inbuilt methods and simple looping.
Using forEach() method
The forEach() method is used on Map in TypeScript to iterate through the key-value pairs and executes the callback function for each element with the arguments as the value, key. This order of iteration is based on the insertion order of the elements.
Syntax:
map.forEach((value: V, key: K, map: Map<K, V>) => {
// code
});
Example: The below code uses the forEach() method to iterate over Map elements in TypeScript.
JavaScript
let geeksMap = new Map<string, string>();
geeksMap.set("DSA","Data Structures and Alogrithms")
geeksMap.set("CP", "Competitive Programming");
geeksMap.set("AI", "Artificial Intelligence");
geeksMap.forEach((value,key) =>{
console.log(`Key: ${key}, Value: ${value}`);
})
Output:
Key: DSA, Value: Data Structures and Algorithms
Key: CP, Value: Competitive Programming
Key: AI, Value: Artificial Intelligence
Using for...of Loop
The for...of loop in TypeScript iterates through the key-value pairs of the Map, and automatically deconstructs each entry into the key and value components.
Syntax:
for (let [key, value] of map) {
// code
}
Example: The below example uses for...of Loop to iterate over Map elements in TypeScript.
JavaScript
let geeksMap = new Map<string, string>();
geeksMap.set("DSA", "Data Structures and Algorithms");
geeksMap.set("CP", "Competitive Programming");
geeksMap.set("AI", "Artificial Intelligence");
for (let [key, value] of geeksMap) {
console.log(`Key: ${key}, Value: ${value}`);
}
Output:
Key: DSA, Value: Data Structures and Algorithms
Key: CP, Value: Competitive Programming
Key: AI, Value: Artificial Intelligence
Using entries() method
The entries() method in TypeScript iterates over the key-value pairs which returns the iterator object that creates the key, and value array of each element in the Map. This returned iterator is then used in the for...of loop to iterate over the Map entries.
Syntax:
let iterator: IterableIterator<[K, V]> = map.entries();
Example: The below uses the entries() method to iterate over Map elements in TypeScript.
JavaScript
let geeksMap = new Map<string, string>();
geeksMap.set("DSA", "Data Structures and Algorithms");
geeksMap.set("CP", "Competitive Programming");
geeksMap.set("AI", "Artificial Intelligence");
for (let [key, value] of geeksMap.entries()) {
console.log(`Key: ${key}, Value: ${value}`);
}
Output:
Key: DSA, Value: Data Structures and Algorithms
Key: CP, Value: Competitive Programming
Key: AI, Value: Artificial Intelligence
Using Array.from() with map.entries()
The combination of Array.from() with map.entries() provides a convenient way to iterate over the key-value pairs of a Map in TypeScript. This approach converts the Map's entries into an array of key-value pairs, which can then be iterated over using array iteration methods.
Syntax:
Array.from(map.entries()).forEach(([key, value]: [K, V]) => {
// code
});
Example: Below is the implementation of the above-discussed approach.
JavaScript
let geeksMap = new Map<string, string>();
geeksMap.set("DSA", "Data Structures and Algorithms");
geeksMap.set("CP", "Competitive Programming");
geeksMap.set("AI", "Artificial Intelligence");
Array.from(geeksMap.entries()).forEach(([key, value]: [string, string]) => {
console.log(`Key: ${key}, Value: ${value}`);
});
Output:
Key: DSA, Value: Data Structures and Algorithms
Key: CP, Value: Competitive Programming
Key: AI, Value: Artificial Intelligence
Using Map.keys() and Map.get() Methods
Another approach to iterate over the Map elements in TypeScript is by using the Map.keys() method in combination with the Map.get() method. This method first retrieves all the keys of the Map using Map.keys(), and then iterates over these keys to access the corresponding values using Map.get(). This approach provides a straightforward way to access both keys and values of the Map.
Example: The below example demonstrates how to use Map.keys() and Map.get() methods to iterate over Map elements in TypeScript.
JavaScript
let map = new Map<string, string>();
map.set("DSA", "Data Structures and Algorithms");
map.set("CP", "Competitive Programming");
map.set("AI", "Artificial Intelligence");
for (let key of map.keys()) {
let value = map.get(key);
console.log(`Key: ${key}, Value: ${value}`);
}
Output:
Key: DSA, Value: Data Structures and Algorithms
Key: CP, Value: Competitive Programming
Key: AI, Value: Artificial Intelligence
Similar Reads
How to Iterate over Enum Values in TypeScript?
Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows: Table of Content Using a for...in loopUsing Object.values()Using a forâ¦o
3 min read
How to iterate over Map elements in JavaScript ?
Map() is very useful in JavaScript it organises elements into key-value pairs. This method iterates over each element in an array and applies a callback function to it, allowing for modifications before returning the updated array." The Map() keyword is used to generate the object. The map() method
3 min read
How to Iterate over Set elements in JavaScript?
We will iterate over set elements in JavaScript. A set is a collection of unique elements i.e. no element can appear more than once in a set. Below are the approaches to iterate over set elements in JavaScript: Approach 1: Using for...of loopThe forâ¦of loop iterates over the iterable objects (like A
3 min read
How to Extract Interface Members in TypeScript ?
In TypeScript, you can extract interface members (properties and methods) from a class using several approaches. we are going to learn how to extract interface members in TypeScript. Below are the approaches used to extract interface members in TypeScript: Table of Content Manual Extractionimplement
3 min read
How to Iterate Over Characters of a String in TypeScript ?
In Typescript, iterating over the characters of the string can be done using various approaches. We can use loops, split() method approach to iterate over the characters. Below are the possible approaches: Table of Content Using for LoopUsing forEach() methodUsing split() methodUsing for...of LoopUs
2 min read
How to Iterate Array of Objects in TypeScript ?
In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method. There are several approaches in TypeScript to iterate over the array of objects which are as follows: Table of Content Using for..
4 min read
How to Iterate Over Object Properties in TypeScript
In TypeScript, Objects are the fundamental data structures that use key-value pair structures to store the data efficiently. To iterate over them is a common task for manipulating or accessing the stored data. TypeScript is a superset of JavaScript and provides several ways to iterate over object pr
3 min read
How to Cast Object to Interface in TypeScript ?
In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU
3 min read
How to Skip Over an Element in .map() in JavaScript?
The .map() method in JavaScript is used for transforming arrays by applying a function to each element. In some cases, we need to skip some elements during the mapping process. The .map() method always returns an array of the same length as the original. This article cover various approaches to skip
2 min read
Iterators & Generators in TypeScript
Iterators and generators are powerful features in TypeScript (and JavaScript) that allow for the creation and manipulation of sequences of values in a lazy, efficient manner, in this article we shall give a detailed account of what these concepts are all about and how to implement them in TypeScript
3 min read