Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Substract value with previus value

I am making app in vue.js and right now I have a little problem

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;
  },

so my total should be Confirmed[i] - Confirmed[i-1] :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you change the forEach to

data.forEach((item, i) =>{

You can access the previous item like so

  this.arrConfirmed.push({date, total: Confirmed - (data[i - 1]?.Confirmed || 0)})

?.Confirmed || 0 is purely for index 0, when there is no previous item

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!