forked from progedu/intro-curriculum-3002
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
42 lines (41 loc) · 1.4 KB
/
app.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
'use strict';
const fs = require('fs');
const readline = require('readline');
const rs = fs.ReadStream('./popu-pref.csv');
const rl = readline.createInterface({ 'input': rs, 'output': {} });
const prefectureDataMap = new Map(); // key: 都道府県 value: 集計データのオブジェクト
rl.on('line', (line) => {
const columns = line.split(',');
const year = columns[0];
const prefecture = columns[2];
const popu = columns[7];
if (year === '2010' || year === '2015') {
let value = prefectureDataMap.get(prefecture);
if (!value) {
value = {
popu10: 0,
popu15: 0,
change: null
};
}
if (year === '2010') {
value.popu10 += parseInt(popu);
}
if (year === '2015') {
value.popu15 += parseInt(popu);
}
prefectureDataMap.set(prefecture, value);
}
});
rl.on('close', () => {
for (let [key, value] of prefectureDataMap) {
value.change = value.popu15 / value.popu10;
}
const rankingArray = Array.from(prefectureDataMap).sort((pair1, pair2) => {
return pair1[1].change - pair2[1].change;
});
const rankingStrings = rankingArray.map(([key, value], i) => {
return (i + 1) + '位 ' + key + ': ' + value.popu10 + '=>' + value.popu15 + ' 変化率:' + value.change;
});
console.log(rankingStrings);
});