From d53d3096ac955c2835f900dd3a1cb5cb5571954e Mon Sep 17 00:00:00 2001 From: Escaity Date: Mon, 27 Jun 2022 21:47:10 +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 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/app.js b/app.js index ad9a93a..3131b36 100644 --- a/app.js +++ b/app.js @@ -1 +1,46 @@ '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; + }); + const rankingStrings = rankingArr.map(([key, value]) => { + return `${key}: ${value.popu10}=>${value.popu15} 変化率: ${value.change}`; + }); + console.log(rankingStrings); +}); From b845f3140d52b65a96026a311886a8b4992c2275 Mon Sep 17 00:00:00 2001 From: Escaity Date: Tue, 28 Jun 2022 19:09:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=89=E5=8C=96=E7=8E=87=E3=83=A9?= =?UTF-8?q?=E3=83=B3=E3=82=AD=E3=83=B3=E3=82=B0=E3=82=92=E6=98=87=E9=A0=86?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 3131b36..e71e4e8 100644 --- a/app.js +++ b/app.js @@ -37,10 +37,11 @@ rl.on('close', () => { //Array.from( )でMapを配列に変換 const rankingArr = Array.from(prefDataMap).sort((pair1, pair2) => { //降順に並び変える - return pair2[1].change - pair1[1].change; + // return pair2[1].change - pair1[1].change; + return pair1[1].change - pair2[1].change; }); - const rankingStrings = rankingArr.map(([key, value]) => { - return `${key}: ${value.popu10}=>${value.popu15} 変化率: ${value.change}`; + const rankingStrings = rankingArr.map(([key, value], i) => { + return `第${i + 1}位 ${key} 変化率: ${value.change}`; }); console.log(rankingStrings); });