Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
'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 map = new Map();
rl.on('line', (lineString) => { // rlオブジェクトでlineイベントが発生した時の処理
const columns = lineString.split(',');
const year = parseInt(columns[0]);
const prefecture = columns[2];
const popu = parseInt(columns[7]);
if (year === 2010 || year === 2015) {
let value = map.get(prefecture);
if (!value) {
value = {
popu10: 0,
popu15: 0,
change: null
};
}
if (year === 2010) {
value.popu10 += popu;
}
if (year === 2015) {
value.popu15 += popu;
}
map.set(prefecture, value);
}
});
rl.resume();
rl.on('close', () => {
for (let pair of map) {
const value = pair[1];
value.change = value.popu15 / value.popu10;
}
// mapを配列に変換後、sort関数で並び替え
const rankingArray = Array.from(map).sort((pair1, pair2) => { // 戻り値が正の時に順番を入れ替える
return pair1[1].change - pair2[1].change;
})
const rankingStrings = rankingArray.map((pair, i) => {
return (i + 1) + "位:" + pair[0] + ': ' + pair[1].popu10 + '=>' + pair[1].popu15 + ' 変化率:' + pair[1].change;
})
console.log(rankingStrings);
})