forked from nnn-training/adding-up
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (43 loc) · 1.3 KB
/
Copy pathapp.js
File metadata and controls
43 lines (43 loc) · 1.3 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
'use strict';
const fs = require('node:fs');
const readline = require('node:readline');
const rs = fs.createReadStream('./popu-pref.csv');
const rl = readline.createInterface({ input: rs });
const prefectureDataMap = new Map(); // ley: 都道府県 value: 集計データのオブジェクト
rl.on('line', lineString => {
const columns = lineString.split(',');
const year = parseInt(columns[0]);
const prefecture =columns[1];
const popu = parseInt(columns[3]);
if (year === 2016 || year ===2021){
let value = null;
if (prefectureDataMap.has(prefecture)){
value = prefectureDataMap.get(prefecture);
} else {
value = {
before: 0,
after: 0,
change: null
};
}
if (year ===2016){
value.before = popu;
}
if (year === 2021){
value.after = popu;
}
prefectureDataMap.set(prefecture, value);
}
});
rl.on('close',() => {
for (const [key, value] of prefectureDataMap){
value.change = value.after / value.before;
}
const rankingArray = Array.from(prefectureDataMap).sort((pair1, pair2) => {
return pair2[1].change - pair1[1].change;
});
const rankingStrings = rankingArray.map(([key, value]) => {
return `${key}: ${value.before}=>${value.after} 変化率: ${value.change}`;
});
console.log(rankingStrings);
});