How to Create a Map of Maps in TypeScript


How to Create a Map of Maps in TypeScript ?

Answer

To create a map of maps in TypeScript, you can use the Map data structure. A map of maps allows you to organize data hierarchically, where each key in the outer map points to another map.



✐ Examples

1 Creating a Map of Maps with Integer Keys and Values

We can create a map of maps in TypeScript by using the Map data structure. This example demonstrates creating a map where both the outer and inner maps use integers for keys and values.

For example,

  1. We start by declaring and initializing a map named outerMap where each value is another map.
  2. We populate the inner maps with key-value pairs.
  3. We insert the inner maps into the outer map using appropriate keys.
  4. We print the resulting map of maps to the console to verify the structure.

TypeScript Program

const outerMap = new Map<number, Map<number, number>>();

// Populate the inner maps
const innerMap1 = new Map<number, number>([
    [1, 10],
    [2, 20],
    [3, 30]
]);
const innerMap2 = new Map<number, number>([
    [4, 40],
    [5, 50],
    [6, 60]
]);

// Insert the inner maps into the outer map
outerMap.set(1, innerMap1);
outerMap.set(2, innerMap2);

// Print the resulting map of maps
console.log('Map of maps with integer keys and values:');
outerMap.forEach((innerMap, outerKey) => {
    console.log(`Outer key: ${outerKey}`);
    innerMap.forEach((value, innerKey) => {
        console.log(`  Inner key: ${innerKey}, value: ${value}`);
    });
});

Output

Map of maps with integer keys and values:
Outer key: 1
  Inner key: 1, value: 10
  Inner key: 2, value: 20
  Inner key: 3, value: 30
Outer key: 2
  Inner key: 4, value: 40
  Inner key: 5, value: 50
  Inner key: 6, value: 60

2 Creating a Map of Maps with Mixed Key and Value Types

We can create a map of maps in TypeScript by using the Map data structure. This example demonstrates creating a map where the outer map uses strings for keys and the inner maps use integers for keys and strings for values.

For example,

  1. We start by declaring and initializing a map named outerMap where each value is another map with mixed key and value types.
  2. We populate the inner maps with key-value pairs.
  3. We insert the inner maps into the outer map using appropriate keys.
  4. We print the resulting map of maps to the console to verify the structure.

TypeScript Program

const outerMap = new Map<string, Map<number, string>>();

// Populate the inner maps
const innerMap1 = new Map<number, string>([
    [1, 'one'],
    [2, 'two'],
    [3, 'three']
]);
const innerMap2 = new Map<number, string>([
    [4, 'four'],
    [5, 'five'],
    [6, 'six']
]);

// Insert the inner maps into the outer map
outerMap.set('first', innerMap1);
outerMap.set('second', innerMap2);

// Print the resulting map of maps
console.log('Map of maps with mixed key and value types:');
outerMap.forEach((innerMap, outerKey) => {
    console.log(`Outer key: ${outerKey}`);
    innerMap.forEach((value, innerKey) => {
        console.log(`  Inner key: ${innerKey}, value: ${value}`);
    });
});

Output

Map of maps with mixed key and value types:
Outer key: first
  Inner key: 1, value: one
  Inner key: 2, value: two
  Inner key: 3, value: three
Outer key: second
  Inner key: 4, value: four
  Inner key: 5, value: five
  Inner key: 6, value: six

3 Creating a Map of Maps with String Keys and Double Values

We can create a map of maps in TypeScript by using the Map data structure. This example demonstrates creating a map where both the outer and inner maps use strings for keys and numbers for values.

For example,

  1. We start by declaring and initializing a map named outerMap where each value is another map with strings for keys and numbers for values.
  2. We populate the inner maps with key-value pairs.
  3. We insert the inner maps into the outer map using appropriate keys.
  4. We print the resulting map of maps to the console to verify the structure.

TypeScript Program

const outerMap = new Map<string, Map<string, number>>();

// Populate the inner maps
const innerMap1 = new Map<string, number>([
    ['A', 1.1],
    ['B', 2.2],
    ['C', 3.3]
]);
const innerMap2 = new Map<string, number>([
    ['D', 4.4],
    ['E', 5.5],
    ['F', 6.6]
]);

// Insert the inner maps into the outer map
outerMap.set('group1', innerMap1);
outerMap.set('group2', innerMap2);

// Print the resulting map of maps
console.log('Map of maps with string keys and number values:');
outerMap.forEach((innerMap, outerKey) => {
    console.log(`Outer key: ${outerKey}`);
    innerMap.forEach((value, innerKey) => {
        console.log(`  Inner key: ${innerKey}, value: ${value}`);
    });
});

Output

Map of maps with string keys and number values:
Outer key: group1
  Inner key: A, value: 1.1
  Inner key: B, value: 2.2
  Inner key: C, value: 3.3
Outer key: group2
  Inner key: D, value: 4.4
  Inner key: E, value: 5.5
  Inner key: F, value: 6.6

Summary

In this tutorial, we learned How to Create a Map of Maps in TypeScript language with well detailed examples.




More TypeScript Maps Tutorials

  1. How to create an Empty Map in TypeScript ?
  2. How to create a Map with Initial Key-Value Pairs in TypeScript ?
  3. How to Print a Map in TypeScript ?
  4. How to Add a Key-Value Pair to a Map in TypeScript ?
  5. How to Set a Default Value for a Key in a Map in TypeScript ?
  6. How to Update the Value for a Key in a Map in TypeScript ?
  7. How to Check if a Map is Empty in TypeScript ?
  8. How to Check if a Key Exists in a Map in TypeScript ?
  9. How to Check if a Value Exists in a Map in TypeScript ?
  10. How to Get the Value Associated with a Key in a Map in TypeScript ?
  11. How to Remove a Key-Value Pair from a Map in TypeScript ?
  12. How to Remove Key-Value Pairs from a Map Based on Values in TypeScript ?
  13. How to Clear All Key-Value Pairs from a Map in TypeScript ?
  14. How to Iterate Over Keys in a Map in TypeScript ?
  15. How to Iterate Over Values in a Map in TypeScript ?
  16. How to Iterate Over Entries (Key-Value Pairs) in a Map in TypeScript ?
  17. How to Get the Size (Number of Key-Value Pairs) of a Map in TypeScript ?
  18. How to Convert a Map to an Array of Keys in TypeScript ?
  19. How to Convert a Map to an Array of Values in TypeScript ?
  20. How to Convert a Map to an Array of Key-Value Pairs in TypeScript ?
  21. How to Merge Two Maps in TypeScript ?
  22. How to Copy a Map in TypeScript ?
  23. How to Check if Two Maps are Equal in TypeScript ?
  24. How to Sort a Map by Keys in TypeScript ?
  25. How to Sort a Map by Values in TypeScript ?
  26. How to Filter a Map Based on Keys in TypeScript ?
  27. How to Filter a Map Based on Values in TypeScript ?
  28. How to Reduce Values in a Map to a Single Value in TypeScript ?
  29. How to Convert an Array of Key-Value Pairs to a Map in TypeScript ?
  30. How to Convert a Map to a JSON String in TypeScript ?
  31. How to Convert a JSON String to a Map in TypeScript ?
  32. How to Swap Keys and Values in a Map in TypeScript ?
  33. How to Create a Map of Maps in TypeScript ?
  34. How to Iterate Over a Map of Maps in TypeScript ?