[Vuejs]-COVID Data api calculate the daily cases from api that returns commutative cases by subtracting from previous object value

0đź‘Ť

âś…

You can use something like this:

result = data.map((record, index, allRecords) => {
    const previous = allRecords[index - 1] || {
        Confirmed: 0
    }
    return {
        ...record,
        newConfirmed: record.Confirmed - previous.Confirmed
    }
});

Which will give you a result such as this which includes the “newConfirmed” key and value:

[{
    "Country": "United Kingdom",
    "CountryCode": "",
    "Province": "",
    "City": "",
    "CityCode": "",
    "Lat": "0",
    "Lon": "0",
    "Confirmed": 190584,
    "Deaths": 28734,
    "Recovered": 0,
    "Active": 161850,
    "Date": "2020-05-04T00:00:00Z",
    "newConfirmed": 190584
}, {
    "Country": "United Kingdom",
    "CountryCode": "",
    "Province": "",
    "City": "",
    "CityCode": "",
    "Lat": "0",
    "Lon": "0",
    "Confirmed": 194990,
    "Deaths": 29427,
    "Recovered": 0,
    "Active": 165563,
    "Date": "2020-05-05T00:00:00Z",
    "newConfirmed": 4406
}, {
    "Country": "United Kingdom",
    "CountryCode": "",
    "Province": "",
    "City": "",
    "CityCode": "",
    "Lat": "0",
    "Lon": "0",
    "Confirmed": 201101,
    "Deaths": 30076,
    "Recovered": 0,
    "Active": 171025,
    "Date": "2020-05-06T00:00:00Z",
    "newConfirmed": 6111
}]

Leave a comment