Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Map Object

TypeScript Map Object

A Map object stores key-value pairs and remembers the original insertion order of the keys.

Any value (object or primitive) can be used as a key or a value.

Map is a new data structure introduced in ES6; you can refer to ES6 Map and Set。


Creating a Map

TypeScript uses the Map type and the new keyword to create a Map:

let myMap = new Map();

To initialize a Map, you can pass key-value pairs in array format:

let myMap = new Map([

        ["key1", "value1"],

        ["key2", "value2"]

    ]); 

Map-related functions and properties:

Common functions

set(key: K, value: V): this - Adds or updates a key-value pair in the Map.

map.set('key1', 'value1');

get(key: K): V | undefined - Gets a value by key, returning undefined if the key does not exist.



const value = map.get('key1');

**has(key: K): boolean **- Checks whether a specified key exists in the Map.



const exists = map.has('key1');

**delete(key: K): boolean **- Deletes the key-value pair for a specified key, returning true if deletion succeeds, otherwise false.

const removed = map.delete('key1');

**clear(): void **- Clears all key-value pairs from the Map.



map.clear();

size: number - Returns the number of key-value pairs in the Map.



const size = map.size;

Iteration methods

keys(): IterableIterator - Returns an iterator containing all keys in the Map.



for (const key of map.keys()) {

  console.log(key);

}

values(): IterableIterator - Returns an iterator containing all values in the Map.



for (const value of map.values()) {

  console.log(value);

}

entries(): IterableIterator<[K, V]> - Returns an iterator containing all key-value pairs in the Map, where each element is a [key, value] array.



for (const [key, value] of map.entries()) {

  console.log(key, value);

}

forEach(callbackfn: (value: V, key: K, map: Map) => void, thisArg?: any): void - Executes a provided callback function once for each key-value pair in the Map.



map.forEach((value, key) => {

  console.log(key, value);

});

Example

Example

const

map

=

new

Map

<

string

,

number

(

)

;

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

let

nameSiteMapping

=

new

Map

(

)

;

//

Setting up the Map object

nameSiteMapping

.

set

(

"

Google

"

,

1

)

;

nameSiteMapping

.

set

(

"

Runoob

"

,

2

)

;

nameSiteMapping

.

set

(

"

Taobao

"

,

3

)

;

//

Get the value corresponding to the key

console

.

log

(

nameSiteMapping

.

get

(

"

Runoob

"

)

)

;

//

2

//

Checking whether the Map contains a value for a key

console

.

log

(

nameSiteMapping

.

has

(

"

Taobao

"

)

)

;

//

true

console

.

log

(

nameSiteMapping

.

has

(

"

Zhihu

"

)

)

;

//

false

//

Returning the number of key/value pairs in the Map object

console

.

log

(

nameSiteMapping

.

size

)

;

//

3

//

Delete Runoob

console

.

log

(

nameSiteMapping

.

delete

(

"

Runoob

"

)

)

;

//

true

console

.

log

(

nameSiteMapping

)

;

//

Remove all key/value pairs from the Map object

nameSiteMapping

.

clear

(

)

;

//

Clear the Map

console

.

log

(

nameSiteMapping

)

;

Use es6 Compilation:

tsc --target es6 test.ts

Compiling the above code yields the following JavaScript code:

Example - test.js file

let

nameSiteMapping

=

new

Map

(

)

;

//

Setting up the Map object

nameSiteMapping

.

set

(

"

Google

"

,

1

)

;

nameSiteMapping

.

set

(

"

Runoob

"

,

2

)

;

nameSiteMapping

.

set

(

"

Taobao

"

,

3

)

;

//

Get the value corresponding to the key

console

.

log

(

nameSiteMapping

.

get

(

"

Runoob

"

)

)

;

//

40

//

Checking whether the Map contains a value for a key

console

.

log

(

nameSiteMapping

.

has

(

"

Taobao

"

)

)

;

//

true

console

.

log

(

nameSiteMapping

.

has

(

"

Zhihu

"

)

)

;

//

false

//

Returning the number of key/value pairs in the Map object

console

.

log

(

nameSiteMapping

.

size

)

;

//

3

//

Delete Runoob

console

.

log

(

nameSiteMapping

.

delete

(

"

Runoob

"

)

)

;

//

true

console

.

log

(

nameSiteMapping

)

;

//

Remove all key/value pairs from the Map object

nameSiteMapping

.

clear

(

)

;

//

Clear the Map

console

.

log

(

nameSiteMapping

)

;

Executing the above JavaScript code produces the following output:

2

true

false

3

true

Map { 'Google' => 1, 'Taobao' => 3 }

Map {}

Iterating over a Map

Elements in a Map object are inserted in order; we can iterate over the Map object, with each iteration returning a [key, value] array.

TypeScript uses for...of to achieve iteration:

Example - test.ts file

let

nameSiteMapping

=

new

Map

(

)

;

nameSiteMapping

.

set

(

"

Google

"

,

1

)

;

nameSiteMapping

.

set

(

"

Runoob

"

,

2

)

;

nameSiteMapping

.

set

(

"

Taobao

"

,

3

)

;

//

Iterate over the keys in a Map

for

(

let

key

of

nameSiteMapping

.

keys

(

)

)

{

console

.

log

(

key

)

;

}

//

Iterate over the values in a Map

for

(

let

value

of

nameSiteMapping

.

values

(

)

)

{

console

.

log

(

value

)

;

}

//

Iterate over key => value in a Map

for

(

let

entry

of

nameSiteMapping

.

entries

(

)

)

{

console

.

log

(

entry

[

0

]

,

entry

[

1

]

)

;

}

//

Using object destructuring

for

(

let

[

key

,

value

]

of

nameSiteMapping

)

{

console

.

log

(

key

,

value

)

;

}

Use es6 Compilation:

tsc --target es6 test.ts

Compiling the above code yields the following JavaScript code:

Example

let

nameSiteMapping

=

new

Map

(

)

;

nameSiteMapping

.

set

(

"

Google

"

,

1

)

;

nameSiteMapping

.

set

(

"

Runoob

"

,

2

)

;

nameSiteMapping

.

set

(

"

Taobao

"

,

3

)

;

//

Iterate over the keys in a Map

for

(

let

key

of

nameSiteMapping

.

keys

(

)

)

{

console

.

log

(

key

)

;

}

//

Iterate over the values in a Map

for

(

let

value

of

nameSiteMapping

.

values

(

)

)

{

console

.

log

(

value

)

;

}

//

Iterate over key => value in a Map

for

(

let

entry

of

nameSiteMapping

.

entries

(

)

)

{

console

.

log

(

entry

[

0

]

,

entry

[

1

]

)

;

}

//

Using object destructuring

for

(

let

[

key

,

value

]

of

nameSiteMapping

)

{

console

.

log

(

key

,

value

)

;

}

Executing the above JavaScript code produces the following output:

Google

Runoob

Taobao

1

2

3

Google 1

Runoob 2

Taobao 3

Google 1

Runoob 2

Taobao 3