TS — Map object, the difference between Map and dictionary

(1) Basic operations of Map

//初始化Map的键和值,它们可以是任何类型,注意Map中所有的键或所有的值必须是相同类型
let nameList = new Map([
    ["key1", 1],
    ["key2", 2]
])
 
 //通过map.set设置键值对,返回该Map对象
nameList.set("key3", 3)
console.log(nameList)

//通过map.get获取键对应的值,如果不存在,则返回undefined
var getmap = nameList.get("key2")
var getmap2 = nameList.get("key999")
console.log(getmap)
console.log(getmap2)

//通过map.delete()删除键值对,删除则返回true,未删除则返回false
var deletemap = nameList.delete("key2")
console.log(deletemap)
console.log(nameList)

//通过has()判断 Map中是否包含所查找的键值,返回布尔型
var hasmap = nameList.has("key1")
var hasmap2 = nameList.has("key999")
console.log(hasmap)
console.log(hasmap2)

//通过clear()移除所有键值对,清楚Map
var clearmap = nameList.clear()
console.log(nameList)
 

(2) Map iteration

There are many ways to iterate Map

1. Use for...of to iterate and return an array containing key-value pairs

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

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

2. Use forEach to iterate, which will accept a callback function as a parameter

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

myMap.forEach((value, key) => {
  console.log(key, value)
})

3. Use the keys method to iterate over all keys

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

for (let key of myMap.keys()) {
  console.log(key)
}

4. Use the values ​​method to iterate over all values

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

for (let value of myMap.values()) {
  console.log(value)
}

It should be noted that when using for...ofor forEachto iterate, the order of key-value pairs is consistent with the order of addition; when using keysor valuesto iterate, the order has nothing to do with the order of addition.

(3) The difference between Map and dictionary

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

MapIt is a native class in JavaScript and is an iterable collection of key-value pairs, where each key is unique. Its keys and values ​​can be of any type. You can setadd new key-value pairs through the method and getget the value through the method. MapThere are also some special methods and properties, such as sizeproperties, clear()methods and forEach()methods. In TypeScript, we can directly use Mapthe type to define a Map object, for example:

const map = new Map<string, number>();
map.set('apple', 1);
map.set('banana', 2);
map.set('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. Dictionaries are 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 lacks Mapsome special methods and properties. Therefore, in TypeScript, if you need to use these special methods and properties, or need to ensure the order of keys, it is recommended to use them Map. If you simply store key-value pairs, you can use objects or dictionaries.

Guess you like

Origin blog.csdn.net/sjsjsjsj1010/article/details/129948567