Ts Map
Map objects store key-value pairs and remember the original insertion order of keys.
Any value (both objects and primitive values) may be used as either a key or a value.
Map is a new data structure introduced in ES6. You can refer to (#).
* * *
## Creating a Map
TypeScript uses the Map type and the `new` keyword to create a Map:
```typescript
let myMap = new Map();
A Map can be initialized with an array of key-value pairs:
```typescript
let myMap = new Map([ ["key1", "value1"], ["key2", "value2"] ]);
Map-related functions and properties:
* `map.clear()` β Removes all key/value pairs from the Map object.
* `map.set()` β Sets the value for the key in the Map object. Returns the Map object.
* `map.get()` β Returns the value associated with the key, or `undefined` if there is none.
* `map.has()` β Returns a boolean indicating whether a value has been associated with the key in the Map object.
* `map.delete()` β Removes any value associated with the key. Returns `true` if an element in the Map object existed and has been removed, or `false` if the element does not exist.
* `map.size` β Returns the number of key/value pairs in the Map object.
* `map.keys()` - Returns a new Iterator object that contains the keys for each element in the Map object.
* `map.values()` β Returns a new Iterator object that contains the values for each element in the Map object.
* `map.entries()` β Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object.
### Common Functions
**`set(key: K, value: V): this`** - Adds or updates an entry in the Map.
```typescript
map.set('key1', 'value1');
**`get(key: K): V | undefined`** - Returns the value associated with the key, or `undefined` if the key doesn't exist.
```typescript
const value = map.get('key1');
**`has(key: K): boolean`** - Checks whether a value has been associated with the key in the Map.
```typescript
const exists = map.has('key1');
**`delete(key: K): boolean`** - Removes the key/value pair associated with the key. Returns `true` if the element existed and has been removed, otherwise `false`.
```typescript
const removed = map.delete('key1');
**`clear(): void`** - Removes all key/value pairs from the Map.
```typescript
map.clear();
**`size: number`** - Returns the number of key/value pairs in the Map.
```typescript
const size = map.size;
### Iteration Methods
**`keys(): IterableIterator`** - Returns a new Iterator object containing the keys for each element in the Map.
```typescript
for (const key of map.keys()) { console.log(key); }
**`values(): IterableIterator`** - Returns a new Iterator object containing the values for each element in the Map.
```typescript
for (const value of map.values()) { console.log(value); }
**`entries(): IterableIterator`** - Returns a new Iterator object containing the [key, value] pairs for each element in the Map.
```typescript
for (const [key, value] of map.entries()) { console.log(key, value); }
**`forEach(callbackfn: (value: V, key: K, map: Map) => void, thisArg?: any): void`** - Executes the provided callback once for each key/value pair in the Map.
```typescript
map.forEach((value, key) => { console.log(key, value); });
### Example
## Example
```typescript
const map = new Map();
map.set('one', 1);
map.set('two', 2);
console.log(map.get('one')); // Output: 1
console.log(map.has('two')); // Output: true
map.delete('one');
console.log(map.size); // Output: 1
map.forEach((value, key) => {
console.log(key, value); // Output: two 2
});
map.clear();
console.log(map.size); // Output: 0
## Example - test.ts file
```typescript
let nameSiteMapping = new Map();
nameSiteMapping.set("Google", 1);
nameSiteMapping.set("", 2);
nameSiteMapping.set("Taobao", 3);
console.log(nameSiteMapping.get(""));
console.log(nameSiteMapping.has("Taobao"));
console.log(nameSiteMapping.has("Zhihu"));
console.log(nameSiteMapping.size);
console.log(nameSiteMapping.delete(""));
console.log(nameSiteMapping);
nameSiteMapping.clear();
console.log(nameSiteMapping);
Compile using **es6**:
```bash
tsc --target es6 test.ts
Compiling the above code produces the following JavaScript code:
## Example - test.js file
```javascript
let nameSiteMapping = new Map();
nameSiteMapping.set("Google", 1);
nameSiteMapping.set("", 2);
nameSiteMapping.set("Taobao", 3);
console.log(nameSiteMapping.get(""));
console.log(nameSiteMapping.has("Taobao"));
console.log(nameSiteMapping.has("Zhihu"));
console.log(nameSiteMapping.size);
console.log(nameSiteMapping.delete(""));
console.log(nameSiteMapping);
nameSiteMapping.clear();
console.log(nameSiteMapping);
Executing the above JavaScript code yields the following output:
2
true
false
3
true
Map { 'Google' => 1, 'Taobao' => 3 }
Map {}
### Iterating a Map
Elements in a Map object are inserted in order. We can iterate over a Map object, with each iteration returning a [key, value] array.
TypeScript uses `for...of` to iterate:
## Example - test.ts file
```typescript
let nameSiteMapping = new Map();
nameSiteMapping.set("Google", 1);
nameSiteMapping.set("", 2);
nameSiteMapping.set("Taobao", 3);
for (let key of nameSiteMapping.keys()) {
console.log(key);
}
for (let value of nameSiteMapping.values()) {
console.log(value);
}
for (let entry of nameSiteMapping.entries()) {
console.log(entry, entry);
}
for (let [key, value] of nameSiteMapping) {
console.log(key, value);
}
Compile using **es6**:
```bash
tsc --target es6 test.ts
Compiling the above code produces the following JavaScript code:
## Example
```javascript
let nameSiteMapping = new Map();
nameSiteMapping.set("Google", 1);
nameSiteMapping.set("", 2);
nameSiteMapping.set("Taobao", 3);
for (let key of nameSiteMapping.keys()) {
console.log(key);
}
for (let value of nameSiteMapping.values()) {
console.log(value);
}
for (let entry of nameSiteMapping.entries()) {
console.log(entry, entry);
}
for (let [key, value] of nameSiteMapping) {
console.log(key, value);
}
Executing the above JavaScript code yields the following output:
Google
Taobao
1
2
3
Google 1
2
Taobao 3
Google 1
2
Taobao 3
YouTip