From af72e8ff66cc052029d75f8ca94667f88b245348 Mon Sep 17 00:00:00 2001 From: yasu-ryo Date: Mon, 24 Jan 2022 14:23:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=A9=E3=83=B3=E3=82=AD=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=81=AE=E5=AE=9F=E8=A3=85=E5=AE=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..a2c32c10 100644 --- a/app.js +++ b/app.js @@ -1 +1,44 @@ 'use strict'; +const fs = require('fs'); +const readline = require('readline'); +const rs = fs.createReadStream('./popu-pref.csv'); +const rl = readline.createInterface({ input: rs, output: {} }); +const prefectureDataMap = new Map(); // key: 都道府県 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 === 2010 || year === 2015) { + let value = prefectureDataMap.get(prefecture); + if (!value) { // 都道府県のデータがなかった場合 + // データの初期化 + value = { + popu10: 0, + popu15: 0, + change: null + }; + } + if (year === 2010) { + value.popu10 = popu; + } + if (year === 2015) { + value.popu15 = 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 pair2[1].change - pair1[1].change; + }); + const rankingStrings = rankingArray.map(([key, value]) => { + return ( + key + ': ' + value.popu10 + '=>' + value.popu15 + ' 変化率:' + value.change + ); + }) + console.log(rankingStrings); +}); \ No newline at end of file From 74f70fd02a7bb001868baad147d83338baeda8af Mon Sep 17 00:00:00 2001 From: yasu-ryo Date: Mon, 24 Jan 2022 14:28:43 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BA=BA=E3=81=8C=E6=B8=9B=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E5=89=B2=E5=90=88=E3=81=AE=E3=83=A9=E3=83=B3=E3=82=AD?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E3=81=AB=E6=94=B9=E5=A4=89=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index a2c32c10..c67eba13 100644 --- a/app.js +++ b/app.js @@ -33,12 +33,10 @@ rl.on('close', () => { value.change = value.popu15 / value.popu10; } const rankingArray = Array.from(prefectureDataMap).sort((pair1, pair2) => { - return pair2[1].change - pair1[1].change; + return pair1[1].change - pair2[1].change; }); - const rankingStrings = rankingArray.map(([key, value]) => { - return ( - key + ': ' + value.popu10 + '=>' + value.popu15 + ' 変化率:' + value.change - ); + const rankingStrings = rankingArray.map(([key, value], i) => { + return (i + 1) + "位" + key + ': ' + value.popu10 + '=>' + value.popu15 + ' 変化率:' + value.change; }) console.log(rankingStrings); }); \ No newline at end of file