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

176
Views
Restar valor con valor anterior

Estoy creando una aplicación en vue.js y ahora mismo tengo un pequeño problema.

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

entonces mi total debe ser Confirmado[i] - Confirmado[i-1] :)

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

0

Si cambia el forEach a

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

Puede acceder al elemento anterior así

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

?.Confirmed || 0 es puramente para el índice 0, cuando no hay un elemento anterior

 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!