From ab2d26c7f98731bcddb0fd24665b9d2af2c3e4dc Mon Sep 17 00:00:00 2001 From: koba Date: Fri, 1 Mar 2024 15:35:43 +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=E5=AE=8C=E4=BA=86(=E3=81=BB?= =?UTF-8?q?=E3=81=BC=E3=82=B3=E3=83=94=E3=83=9A())?= 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 ad9a93a..2b10353 100644 --- a/app.js +++ b/app.js @@ -1 +1,51 @@ 'use strict'; +const fs = require('node:fs'); +const readline = require('node:readline'); +const rs = fs.createReadStream('./popu-pref.csv'); +const rl = readline.createInterface({ input: rs }); +const prefDtMap = 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 === 2016 || year === 2021) { + let value = null; + if (prefDtMap.has(prefecture)) { + value = prefDtMap.get(prefecture); + } else { + value = { + before: 0, + after: 0, + change: null + }; + } + if (year === 2016) { + value.before = popu; + } + if (year === 2021) { + value.after = popu; + } + prefDtMap.set(prefecture, value); + } +}); + +rl.on('close', () => { + + + for(const [key, val] of prefDtMap){ + val.change = val.after / val.before; + } + const rankingArray = Array.from(prefDtMap).sort((pair1, pair2) => { + return pair1[1].change - pair2[1].change; + }); + + const rankingStrings = rankingArray.map(([key, value],i) => { + return `${i + 1}位${key}: ${value.before} => ${value.after} 変化率: ${value.change}`; + }); + + + console.log(rankingStrings); + +}); \ No newline at end of file