How to merge Maps in JavaScript or TypeScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Table of Contents

  1. Merge Map objects in JavaScript
  2. Merge Maps in TypeScript

# How to merge Map Objects in JavaScript

To merge Map objects:

  1. Use the spread syntax (...) to unpack the values of two or more Map objects into an array.
  2. Pass the array to the Map() constructor.
  3. The new Map will contain the key-value pairs from all provided Map objects.
index.js
const map1 = new Map([['name', 'bobby hadz']]); const map2 = new Map([['age', 30]]); const map3 = new Map([...map1, ...map2]); // ๐Ÿ‘‡๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map3);

merge map objects in javascript

The code for this article is available on GitHub

We used the spread syntax (...) to unpack the key-value pairs of 2 Map objects into an array.

index.js
const map1 = new Map([['name', 'bobby hadz']]); const map2 = new Map([['age', 30]]); // ๐Ÿ‘‡๏ธ [ [ 'name', 'bobby hadz' ], [ 'age', 30 ] ] console.log([...map1, ...map2]);

The array contains nested arrays consisting of key-value pairs.

The last step is to pass the array to the Map() constructor.

With the values in place, the example looks as follows.

index.js
const map3 = new Map([ ['name', 'bobby hadz'], ['age', 30], ]); // ๐Ÿ‘‡๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map3);

The iterable, the Map() constructor takes must consist of key-value pairs, e.g. a two-dimensional array or another Map object.

This process could be repeated with as many Map objects as necessary.

index.js
const map1 = new Map([['name', 'bobby hadz']]); const map2 = new Map([['age', 30]]); const map3 = new Map([['country', 'Chile']]); const map4 = new Map([...map1, ...map2, ...map3]); // ๐Ÿ‘‡๏ธ Map(3) { 'name' => 'bobby hadz', 'age' => 30, 'country' => 'Chile' } console.log(map4);

Note that the insertion order of keys is preserved. If you need to reorder the key-value pairs, simply switch the order in which the spread syntax (...) is used.

index.js
const map1 = new Map([['name', 'bobby hadz']]); const map2 = new Map([['age', 30]]); const map3 = new Map([...map2, ...map1]); // ๐Ÿ‘‡๏ธ Map(2) { 'age' => 30, 'name' => 'bobby hadz' } console.log(map3);

We unpacked the elements of map2 before the elements of map1 in the example.

# Merge Map Objects using a for...of loop

You can also use a nested for...of loop to merge Map objects.

index.js
function mergeMapObjects(...maps) { const map = new Map(); for (const m of maps) { for (const item of m) { map.set(...item); } } return map; } const map1 = new Map([['name', 'bobby hadz']]); const map2 = new Map([['age', 30]]); const map3 = mergeMapObjects(map1, map2); // ๐Ÿ‘‡๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map3);

merge map objects using for of loop

The code for this article is available on GitHub

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

The function takes one or more Map objects as parameters and uses a for...of loop to iterate over them.

The outer for...of loop iterates over the Map objects and the inner for...of loop iterates over the key-value pairs of each Map.

On each iteration, we use the Map.set method to add the current key-value pair to the new Map object.

The code for this article is available on GitHub

# Merge Maps in TypeScript

To merge Maps in TypeScript:

  1. Explicitly type the keys and values of the third Map.
  2. Use the spread syntax (...) to unpack the key-value pairs of the first two Maps.
  3. The types of the keys and values of the Maps have to match.
index.ts
const map1 = new Map([['country', 'Germany']]); const map2 = new Map([['salary', 100]]); const map3 = new Map<string, string | number>([...map1, ...map2]); // ๐Ÿ‘‡๏ธ {'country' => 'Germany', 'salary' => 100} console.log(map3);
The code for this article is available on GitHub

We used a generic to type the keys and values of the third Map object.

We could have done the same for the first two Maps, however, it wasn't needed because TypeScript was able to infer the type based on the passed-in values.

The following code sample achieves the same result.

index.ts
const map1 = new Map<string, string>([['country', 'Germany']]); const map2 = new Map<string, number>([['salary', 100]]); const map3 = new Map<string, string | number>([...map1, ...map2]); // ๐Ÿ‘‡๏ธ {'country' => 'Germany', 'salary' => 100} console.log(map3);
The code for this article is available on GitHub

The first Map has keys of type string and values of type string.

The second Map has keys of type string and values of type number.

When merging the Maps, we have to make sure the type of the third Map conforms with the types of the other two.

The example shows how we had to use a union type to set the values of the third map to be strings or numbers.

We used the spread operator (...) to unpack the key-value pairs from 2 Map objects into an array.

index.ts
const map1 = new Map<string, string>([['country', 'Germany']]); // ๐Ÿ‘‡๏ธ [ ['country', 'Germany'] ] console.log([...map1]);

The array contains nested arrays consisting of key-value pairs.

The last step is to pass the array to the Map() constructor.

With the values in place, the Map looks as follows.

index.ts
const map3 = new Map<string, string | number>([ ['country', 'Germany'], ['salary', 100], ]);

The Map() constructor takes an iterable that must consist of key-value pairs, e.g. a two-dimensional array or another Map object.

This process could be repeated with as many Map objects as necessary, as long as the type of the final Map conforms with the types of the other Maps.

index.ts
const map1 = new Map<string, string>([['country', 'Germany']]); const map2 = new Map<string, number>([['salary', 100]]); const map3 = new Map<string, boolean>([['isProgrammer', true]]); const map4 = new Map<string, string | number | boolean>([ ...map1, ...map2, ...map3, ]); // Map(3) { // 'country' => 'Germany', // 'salary' => 100, // 'isProgrammer' => true // } console.log(map4);
The code for this article is available on GitHub

The type of the values in the fourth Map has to be compatible with the type of the values in all other Maps.

Note that the insertion order of keys is preserved. If you need to re-order the key-value pairs, simply switch the order in which the spread operator (...) is used.

# 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