From 0e8e3461c4c3667248c9040e492fb22143d49bfc Mon Sep 17 00:00:00 2001 From: nakahiro Date: Wed, 11 Aug 2021 16:55:04 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A9=E3=83=B3=E3=82=AD=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..711630bb 100644 --- a/app.js +++ b/app.js @@ -1 +1,51 @@ '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 prefectureDateMap = new Map(); +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 = prefectureDateMap.get(prefecture); + if (!value){ + value = { + popu10:0, + popu15:0, + change:null + }; + } + if(year === 2010){ + value.popu10 = popu; + } + if(year === 2015){ + value.popu15 = popu; + } + prefectureDateMap.set(prefecture, value); + } +}); + + rl.on('close', ()=> { + for (let [key,value] of prefectureDateMap){ + value.change = value.popu15/value.popu10; + } + const rankingArray = Array.from(prefectureDateMap).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