-
Notifications
You must be signed in to change notification settings - Fork 1
/
maps.js
90 lines (70 loc) · 2.64 KB
/
maps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** Maps **/
// Map is a new structure in ES6.
var map = new Map()
map.set('foo', 'Mr.Foo')
map.set('bar', 'Mr.Bar')
console.log(map) // Map { 'foo' => 'Mr.Foo', 'bar' => 'Mr.Bar' }
// We can use anything as a key.
// We are not limited to primitive (i.e. symbols, numbers, or strings).
// We can use functions.
map.set(() => 'key', 'Value') // Map { [Function] => 'Value' }
// We can use objects.
map.set({}, 'Value') // Map { {} => 'Value' }
// We can use dates.
map.set(new Date, 'Value') // Map { 2017-03-16T16:37:15.488Z => 'Value' }
// We can use any object that follows the iterable protocol.
// e.g. ['key', 'value']
var map = new Map([
['foo', 'Mr.Foo'],
['bar', 'Mr.Bar']
])
console.log(map) // Map { 'foo' => 'Mr.Foo', 'bar' => 'Mr.Bar' }
// We can use the spread operator to have the opposite effect.
console.log([...map]) // [ [ 'foo', 'Mr.Foo' ], [ 'bar', 'Mr.Bar' ] ]
// We can use for...of along with destructuring to handle the map.
for (let [key, value] of map) {
console.log(`${key}: ${value}`)
// 'foo: Mr.Foo'
// 'bar: Mr.Bar'
}
// Keys are unique.
// Therefore setting a key that is already defined,
// it will override the existing one.
var map = new Map()
map.set('foo', 'Mr.Foo')
console.log(map) // Map { 'foo' => 'Mr.Foo' }
map.set('foo', 'Mr.Bar')
console.log(map) // Map { 'foo' => 'Mr.Bar' }
// We can check if a key exists using .has().
var map = new Map([['foo', 'Mr.Foo']])
console.log(map.has('foo')) // true
console.log(map.has('bar')) // false
// We can use Symbol as soon as we keep the reference around.
var sym = Symbol()
var map = new Map([[sym, 'Mr.Sym']])
console.log(map.has(sym)) // true
// No casting is done.
var map = new Map([[1, 'Mr.One']])
console.log(map.has('1')) // false
// We can delete elements.
var map = new Map([['foo', 'Mr.Foo'], ['bar', 'Mr.Bar']])
// Using .delete()
map.delete('bar')
console.log(map) // Map { 'foo' => 'Mr.Foo' }
// Using .clear(), which deletes everything
map.clear()
console.log(map) // Map { }
// Iterate over a map.
var map = new Map([['foo', 'Mr.Foo'], ['bar', 'Mr.Bar']])
// Use .entries() to iterate over the map.
console.log(map.entries()) // MapIterator { [ 'foo', 'Mr.Foo' ], [ 'bar', 'Mr.Bar' ] }
// Use .keys() to iterate over the map keys.
console.log(map.keys()) // MapIterator { 'foo', 'bar' }
// Use .values() to iterate over the map values.
console.log(map.values()) // MapIterator { 'Mr.Foo', 'Mr.Bar' }
// We can count the number of entries with .size().
var map = new Map([['foo', 'Mr.Foo'], ['bar', 'Mr.Bar']])
console.log(map.size) // 2
// Btw,
// Map's entries are iterated in insertion order.
// As opposite to Object.keys, which follow an arbitary order.