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
46 changes: 46 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
'use strict';
const fs = require('fs');
const readline = require('readline');
const rs = fs.createReadStream('./popu-pref.csv');
const rl = readline.createInterface({ input: rs });
const prefDataMap = new Map();
rl.on('line', (lineString) => {
const columns = lineString.split(',');
const year = parseInt(columns[0]);
const pref = columns[1];
const popu = parseInt(columns[3]);
if (year === 2010 || year === 2015) {
let value = null;
if (prefDataMap.has(pref)) {
value = prefDataMap.get(pref);
} else {
value = {
popu10: 0,
popu15: 0,
change: null,
};
}
if (year === 2010) {
value.popu10 = popu;
}
if (year === 2015) {
value.popu15 = popu;
}
prefDataMap.set(pref, value);
}
});

rl.on('close', () => {
for (const [key, value] of prefDataMap) {
value.change = value.popu15 / value.popu10;
}
//Array.from( )でMapを配列に変換
const rankingArr = Array.from(prefDataMap).sort((pair1, pair2) => {
//降順に並び変える
// return pair2[1].change - pair1[1].change;
return pair1[1].change - pair2[1].change;
});
const rankingStrings = rankingArr.map(([key, value], i) => {
return `第${i + 1}位 ${key} 変化率: ${value.change}`;
});
console.log(rankingStrings);
});