Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
51 lines (35 loc) · 1.58 KB

File metadata and controls

51 lines (35 loc) · 1.58 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Objects for storing keyed collections.
Arrays for storing ordered collections.
But that’s not enough for real life. That’s why Map and Set also exist.
Map
Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.
The main methods are:
new Map() – creates the map.
map.set(key, value) – stores the value by the key.
map.get(key) – returns the value by the key, undefined if key doesn’t exist in map.
map.has(key) – returns true if the key exists, false otherwise.
map.delete(key) – removes the value by the key.
map.clear() – clears the map
map.size – returns the current element count. */
// EXAMPLE-1
let map = new Map(),
val2 = 'val2',
val3 = {
key: 'value'
};
console.log(map) // => Map {}
map.set(0, 'val-1')
console.log(map) // => Map { 0 => 'val-1' }
// EXAMPLE-2 Map can also use objects as keys
let john = { name: "John" };
// for every user, let's store his visits count
let visitsCountMap = new Map();
// john is the key for the map
visitsCountMap.set(john, 123);
// console.log(visitsCountMap.get(john) ); // 123
console.log(visitsCountMap) // => Map { { name: 'John' } => 123 }
/* Using objects as keys is one of most notable and important Map features. For string keys, Object can be fine, but it would be difficult to replace the Map with a regular Object in the example above. In the old times, before Map existed, people added unique identifiers to objects for that like below */
let john_old = { name: 'john-old', id : 1}
let visitsCounts = {}
visitsCounts[john.id] = 123;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.