File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
2+ const fs = require ( 'fs' ) ;
3+ const readline = require ( 'readline' ) ;
4+ const rs = fs . ReadStream ( './popu-pref.csv' ) ;
5+ const rl = readline . createInterface ( { 'input' :rs , 'output' : { } } ) ;
6+ const map = new Map ( ) ; // key: 都道府県 value: 集計データのオブジェクト
7+ rl . on ( 'line' , ( lineString ) => {
8+ const columns = lineString . split ( ',' ) ;
9+ const year = parseInt ( columns [ 0 ] ) ;
10+ const prefecture = columns [ 2 ] ;
11+ const popu = parseInt ( columns [ 7 ] ) ;
12+ if ( year === 2010 || year === 2015 ) {
13+ let value = map . get ( prefecture ) ;
14+ if ( ! value ) {
15+ value = {
16+ popu10 : 0 ,
17+ popu15 : 0 ,
18+ change : null
19+ } ;
20+ }
21+ if ( year === 2010 ) {
22+ value . popu10 += popu ;
23+ }
24+ if ( year === 2015 ) {
25+ value . popu15 += popu ;
26+ }
27+ map . set ( prefecture , value ) ;
28+ }
29+ } ) ;
30+ rl . resume ( ) ;
31+ rl . on ( 'close' , ( ) => {
32+ for ( let pair of map ) {
33+ const value = pair [ 1 ] ;
34+ value . change = value . popu15 / value . popu10 ;
35+ }
36+ const rankingArray = Array . from ( map ) . sort ( ( pair1 , pair2 ) => {
37+ return pair2 [ 1 ] . change - pair1 [ 1 ] . change ;
38+ } ) ;
39+ const rankingStrings = rankingArray . map ( ( pair ) => {
40+ return pair [ 0 ] + ': ' + pair [ 1 ] . popu10 + '=>' + pair [ 1 ] . popu15 + '変化率:' + pair [ 1 ] . change ;
41+ } ) ;
42+ console . log ( rankingStrings ) ;
43+ } ) ;
You can’t perform that action at this time.
0 commit comments