-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.js
47 lines (46 loc) · 877 Bytes
/
dictionary.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
class Dictionary {
constructor() {
this.items = {};
}
has(key) {
return key in this.items;
}
set(key, value) {
this.items[key] = value;
}
remove(key) {
if (this.has(key)) {
delete this.items[key];
return true;
}
return false;
}
get(key) {
return this.has(key) ? this.items[key] : undefined;
}
values() {
let values = [];
for (let key in this.items) {
if (this.has(key)) {
values.push(this.items[key]);
}
}
return values; // return Object.values(this.items)도 가능(ES8)
}
clear() {
this.items = {};
}
size() {
return Object.keys(this.items).length;
}
keys() {
let values = [];
for (let key in this.items) {
values.push(key);
}
return values; // return Object.keys(this.items)도 가능
}
getItmes() {
return this.items;
}
}