-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
51 lines (37 loc) · 1.2 KB
/
map.js
File metadata and controls
51 lines (37 loc) · 1.2 KB
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
// JavaScript map()
// sets deal with arrays
// maps deal with objects
// map has a key value pair
// same way to create set except you use map keyword
const people = new Map();
// first argument is the key. The second argument is the value
// the values added have to be unique. Each item has to be unique
people.set('Jason', 1):
people.set('Nathan', 1):
people.set('Jared', 1):
// .has method returns a boolean
// .get will get the value that is in the .get()
console.log(people.has('Jason')); // returns a boolean
// console.log(people.get('Jared'));
**********************************************************************
// delete items individually
const people = new Map();
people.set('Jason', 1):
people.set('Nathan', 1):
people.set('Jared', 1):
people.delete('Jared');
// people.clear();
console.log(people);
********************************************************************
// two ways to loop over a map
// first way we can use a for each loop
//
const people = new Map();
people.set('Jason', 1):
people.set('Nathan', 1):
people.set('Jared', 1):
// console.log(people);
// people.forEach((item) => console.log(item));
for(const [key, value] of people) {
console.log(key, value);
};