我正在用vue.js制作应用程序,现在我有个小问题
async fetchCovidDataByDay(){
const res = await fetch(`https://api.covid19api.com/live/country/${this.name}/status/confirmed`);
const data = await res.json();
this.arrConfirmed= [];
this.arrDeaths = [];
this.arrRecovered = [];
this.arrActive = [];
data.forEach(item =>{
const date = moment(item.Date).format('MMMM Do YYYY');
const {
Confirmed,
Deaths,
Recovered,
Active
} = item;
this.arrConfirmed.push({date, total: Confirmed})
this.arrDeaths.push({date, total: Deaths})
this.arrRecovered.push({date, total: Recovered})
this.arrActive.push({date, total: Active})
})
return data;
},所以我的总数应该是康菲尼迪-康菲尼迪-1 :)
发布于 2022-06-16 09:13:44
如果您将forEach更改为
data.forEach((item, i) =>{您可以访问前面的项目,如下所示
this.arrConfirmed.push({date, total: Confirmed - (data[i - 1]?.Confirmed || 0)})当没有前一项时,?.Confirmed || 0纯粹用于索引0。
const data = [{Confirmed: 100}, {Confirmed: 120}, {Confirmed: 180}, {Confirmed: 300}];
const arrConfirmed= [];
let prevConfirmed = 0;
data.forEach((item, i) =>{
const { Confirmed, } = item;
arrConfirmed.push({total: Confirmed - (data[i - 1]?.Confirmed || 0)});
});
console.log(arrConfirmed);
https://stackoverflow.com/questions/72643231
复制相似问题