8000 Implove examples · HowProgrammingWorks/Map@4e9400a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e9400a

Browse files
committed
Implove examples
1 parent 928ae7f commit 4e9400a

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

JavaScript/1-array.js renamed to JavaScript/1-object.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Dictionary {
44
constructor() {
5-
this.map = {};
5+
this.map = Object.create(null);
66
}
77
set(key, value) {
88
return this.map[key] = value;
@@ -11,7 +11,7 @@ class Dictionary {
1111
return this.map[key];
1212
}
1313
has(key) {
14-
return this.map.hasOwnProperty(key);
14+
return !!this.map[key];
1515
}
1616
delete(key) {
1717
delete this.map[key];
@@ -23,7 +23,7 @@ class Dictionary {
2323
return Object.keys(this.map);
2424
}
2525
clear() {
26-
this.map = {};
26+
this.map = Object.create(null);
2727
}
2828
}
2929

JavaScript/2-map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ if (!cityPopulation.has('Shanghai')) {
1818
}
1919

2020
console.log('size:', cityPopulation.size);
21-
console.log('keys:', cityPopulation.keys());
21+
console.log('keys:', [...cityPopulation.keys()]);

JavaScript/5-index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ const buildIndex = (ds, col) => {
2020
// Usage
2121

2222
const dataset = getDataset('./cities.dat');
23+
console.log(dataset);
24+
2325
const byName = buildIndex(dataset, 0);
26+
console.log(byName);
27+
2428
const byPopulation = buildIndex(dataset, 1);
29+
console.log(byPopulation);
2530

2631
const delhi = byName.get('Delhi');
2732
console.log(delhi);

0 commit comments

Comments
 (0)
0