How to Iterate over a Map in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
3 min

banner

# Iterate over a Map in TypeScript

Use the forEach() method to iterate over a Map in TypeScript. The forEach method takes a function that gets invoked for each key-value pair in the Map.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); map1.forEach((value, key) => { // Bobby Hadz name // Germany country // 30 age console.log(value, key); });

iterate over map in typescript

The code for this article is available on GitHub

We used a generic to type the Map when declaring it.

We set the Map's keys to have a type of string and its values to be of type string or number.

We used a union type when typing the values of the Map. Note that you have to include all of the possible types of values the Map object will store.

index.ts
const map1 = new Map<string, string | number | number[]>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); // ๐Ÿ‘‡๏ธ add an array of numbers to the Map map1.set('years', [2024, 2025, 2026]); map1.forEach((value, key) => { // Bobby Hadz name // Germany country // 30 age // [ 2024, 2025, 2026 ] years console.log(value, key); });

The code sample adds an array of numbers to the Map's values, so we had to include number[] in the possible types when declaring the Map object.

We used the Map.forEach method to iterate over the key-value pairs of the Map.

The function we passed to the method gets called with the following parameters:

  • the value of the current iteration.
  • the key of the current iteration.
  • the Map object that's being iterated.

The forEach() method returns undefined.

I've also written an article on how to iterate over an object with forEach() in TS.

# Iterate over a Map in TypeScript using for...of

You can also use a for...of loop to iterate over a Map object in TypeScript.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); for (const [key, value] of map1) { // name Bobby Hadz // country Germany // age 30 console.log(key, value); }

iterate over map in typescript using for of

The code for this article is available on GitHub

The for...of loop allows us to iterate over iterable objects like Maps, Sets and arrays.

We used destructuring assignment when declaring the key and value variables.

index.ts
const [key, value] = ['Bobby Hadz', 'Germany']; console.log(key); // ๐Ÿ‘‰๏ธ Bobby Hadz console.log(value); // ๐Ÿ‘‰๏ธ Germany

The for...of loop might be your preferred approach if you have to use the break keyword to exit the loop prematurely.

Using the break keyword is not supported in the forEach method.

The for...of loop iterates only over an object's own properties, as opposed to the for...in loop which also iterates over inherited properties.

# Iterate over a Map object's Keys or Values in TypeScript

You can use the Map.keys() and Map.values() methods to get iterable objects that contain the keys and values of the Map.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); // โœ… Iterate over a Map's keys for (const key of map1.keys()) { console.log(key); // ๐Ÿ‘‰๏ธ name, country, age } // โœ… Iterate over a Map's values for (const value of map1.values()) { console.log(value); // ๐Ÿ‘‰๏ธ Bobby Hadz, Germany, 30 }

iterate over map object keys and values

The code for this article is available on GitHub

Note that the return values from the Map.keys() and Map.values() methods are arrays, they are iterator objects.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); // ๐Ÿ‘‡๏ธ const keys: IterableIterator<string> const keys = map1.keys(); // ๐Ÿ‘‡๏ธ const values: IterableIterator<string | number> const values = map1.values();

# Converting the iterator objects to arrays

If you want to convert the values to an array, e.g. to use the forEach method, use the Array.from() method.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); // ๐Ÿ‘‡๏ธ const keys: string[] const keys = Array.from(map1.keys()); // ๐Ÿ‘‡๏ธ const values: (string | number)[] const values = Array.from(map1.values());
The code for this article is available on GitHub

An alternative approach is to use the spread operator (...) to unpack the values from the iterator object into an array.

index.ts
const map1 = new Map<string, string | number>([ ['name', 'Bobby Hadz'], ['country', 'Germany'], ['age', 30], ]); // ๐Ÿ‘‡๏ธ const keys: string[] const keys = [...map1.keys()]; // ๐Ÿ‘‡๏ธ const values: (string | number)[] const values = [...map1.values()];

The last 2 examples achieve the same result. They both convert the iterator objects to an array.

You can now use the forEach method to iterate over the arrays, as well as any other array built-in methods.

I've also written an article on how to define a Map with Array values in TS.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev