Map
Examples are in TypeScript.

Use function setters and getters

1
const map: Map<string, string> = new Map();
2
// Do not use []
3
// This is bad
4
map["key"] = "value";
5
6
// Do this instead
7
map.get(key, value);
Copied!

Iterating Map with for..of

1
var myMap = new Map();
2
myMap.set(0, 'zero');
3
myMap.set(1, 'one');
4
for (var [key, value] of myMap) {
5
console.log(key + ' = ' + value);
6
}
7
// 0 = zero
8
// 1 = one
9
10
for (var key of myMap.keys()) {
11
console.log(key);
12
}
13
// 0
14
// 1
15
16
for (var value of myMap.values()) {
17
console.log(value);
18
}
19
// zero
20
// one
21
22
for (var [key, value] of myMap.entries()) {
23
console.log(key + ' = ' + value);
24
}
25
// 0 = zero
26
// 1 = one
Copied!
Last modified 2yr ago