@@ -7,16 +7,46 @@ const deathsUrl = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/mas
7
7
8
8
let get_combine_data = ( ) => {
9
9
return Promise . all ( [ get_data ( casesUrl ) , get_data ( recoveredUrl ) , get_data ( deathsUrl ) ] )
10
- . then ( result => {
11
- //combine the data
10
+ . then ( ( [ reportedData , recoveredData , deathsData ] ) => { // spread the response to 3 arrays
11
+ // get the dates from the first object of reportedData
12
+ const [ province , country , lat , long , ...dates ] = Object . keys ( reportedData [ 0 ] )
13
+
14
+ // loop through the reported data
15
+ reportedData . forEach ( element => {
16
+ // for each country
17
+ // the first 4 values are province, country, lat, long and the rest counts of cases per date
18
+ const [ province , country , lat , long , ...counts ] = Object . values ( element )
19
+ // find data of each country in the other 2 arrays
20
+ let recov = getCountryData ( recoveredData , country )
21
+ let dths = getCountryData ( deathsData , country )
22
+
23
+ // loop though the dates
24
+ dates . forEach ( ( date , i ) => {
25
+ let reported = parseInt ( counts [ i ] )
26
+ let recovered = recov . hasOwnProperty ( 'Country/Region' ) ? parseInt ( recov [ date ] ) : 0
27
+ let deaths = dths . hasOwnProperty ( 'Country/Region' ) ? parseInt ( dths [ date ] ) : 0
28
+
29
+ element [ date ] = {
30
+ reported,
31
+ recovered,
32
+ deaths
33
+ } ;
34
+ } )
35
+ } )
12
36
13
37
// return combined data
14
- return result
38
+ return reportedData
15
39
} )
16
40
. catch ( error => [ {
17
41
"message" : "unable to load the data" ,
18
42
'error' : error
19
43
} ] )
20
44
}
21
45
46
+ // get country data if it exists in the passed array
47
+ let getCountryData = ( countriesArray , country ) => {
48
+ let countryData = { }
49
+ return countriesArray . find ( element => element [ 'Country/Region' ] . toString ( ) . toLowerCase ( ) === country . toLowerCase ( ) )
50
+ }
51
+
22
52
module . exports = get_combine_data ;
0 commit comments