Skip to content

Commit 51b4dab

Browse files
combined the data
1 parent eb5a9a5 commit 51b4dab

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

helpers/combined-data.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,46 @@ const deathsUrl = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/mas
77

88
let get_combine_data = () => {
99
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+
})
1236

1337
// return combined data
14-
return result
38+
return reportedData
1539
})
1640
.catch(error => [{
1741
"message": "unable to load the data",
1842
'error': error
1943
}])
2044
}
2145

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+
2252
module.exports = get_combine_data;

0 commit comments

Comments
 (0)