SoFunction
Updated on 2025-02-28

Detailed explanation of the use of Map objects in TypeScript and the difference between Map and dictionary

(1) Basic operation of Map

//Initialize the keys and values ​​of the Map, they can be of any type. Note that all keys or all values ​​in the Map must be of the same typelet nameList = new Map([
    ["key1", 1],
    ["key2", 2]
])
 
 //Return the Map object by setting key-value pairs("key3", 3)
(nameList)

//By getting the corresponding value of the key, if it does not exist, return undefinedvar getmap = ("key2")
var getmap2 = ("key999")
(getmap)
(getmap2)

//Delete the key-value pair through () and return true if deleted. If not deleted, return false if not deleted.var deletemap = ("key2")
(deletemap)
(nameList)

//Display through has() to determine whether the searched key value is included in the map, and return to the Boolean typevar hasmap = ("key1")
var hasmap2 = ("key999")
(hasmap)
(hasmap2)

//Remove all key-value pairs through clear() to understand Mapvar clearmap = ()
(nameList)
 

(2) Map iteration

There are many ways to iterate Map

1. Iterate with for...of and return an array containing key-value pairs

let myMap = new Map()
(0, "zero")
(1, "one")

for (let [key, value] of myMap) {
  (key, value)
}

2. Iterate with forEach, it will accept a callback function as an argument

let myMap = new Map()
(0, "zero")
(1, "one")

((value, key) => {
  (key, value)
})

3. Iterate over all keys using the keys method

let myMap = new Map()
(0, "zero")
(1, "one")

for (let key of ()) {
  (key)
}

4. Iterate over all values ​​using the values ​​method

let myMap = new Map()
(0, "zero")
(1, "one")

for (let value of ()) {
  (value)
}

It should be noted that usefor...oforforEachWhen iterating, the order of key-value pairs is consistent with the order of addition; and usekeysorvaluesWhen iterating, the order has nothing to do with the order of addition.

(3) The difference between Map and Dictionary

In TypeScript,MapBoth dictionaries (also called associative arrays or hash tables) can be used to store key-value pairs. The main difference between them is in the internal implementation and some features.

Mapis a native class in JavaScript, a collection of iterable key-value pairs, where each key is unique. Its keys and values ​​can be of any type and can be passedsetMethod to add new key-value pairs, throughgetMethod to get the value.MapThere are also some special methods and attributes, such assizeAttributes,clear()Methods andforEach()Methods, etc. In TypeScript, we can use it directlyMapType to define a Map object, for example:

const map = new Map<string, number>();
('apple', 1);
('banana', 2);
('orange', 3);

A dictionary (or associative array or hash table) is a common data structure that can also be used to store key-value pairs, where each key is also unique. The implementation method of dictionary is generally implemented through hash tables. In TypeScript, we usually use objects to simulate dictionaries, for example:

const dict = { 
  apple: 1,
  banana: 2,
  orange: 3,
};

Although dictionary may be more efficient in implementation, it is not a native class and lacksMapSome special methods and attributes of  . Therefore, in TypeScript, if you need to use these special methods and attributes, or you need to ensure the order of keys, it is recommended to useMap. If you just store key-value pairs, you can use objects or dictionaries.

Summarize

This is the article about the use of Map objects in TypeScript and the difference between Map and dictionary. This is all about this article. For more information about the differences between TS Map objects and dictionaries, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!