-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path5-index.js
More file actions
35 lines (26 loc) · 703 Bytes
/
Copy path5-index.js
File metadata and controls
35 lines (26 loc) · 703 Bytes
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
'use strict';
const fs = require('node:fs');
const getDataset = (file) => {
const lines = fs.readFileSync(file, 'utf8').split('\n');
lines.shift();
lines.pop();
return lines.map((line) => line.split(','));
};
const buildIndex = (ds, col) => {
const index = new Map();
for (const record of ds) {
index.set(record[col], record);
}
return index;
};
// Usage
const dataset = getDataset('./cities.csv');
console.log(dataset);
const byName = buildIndex(dataset, 0);
console.log(byName);
const byPopulation = buildIndex(dataset, 1);
console.log(byPopulation);
const delhi = byName.get('Delhi');
console.log(delhi);
const record = byPopulation.get('21516000');
console.log(record);