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

211
Views
how to calculate the difference between current and previous values? should I use reduce?

So I have a an array ob objects (recentHistory) and each stores a value - an exchange rate for a particular date.

Now I'm writing a getter that should be able to calculate the difference between the rates of day1 and day2, day2 and day3, day3 and day4 etc.

  public get change(): number  {
    //return this.store.recentHistory[0].rate - this.store.recentHistory[1].rate;
    this.store.recentHistory.reduce((prev, item, index) => {
      return prev.rate - item.rate /????
    })

The commented line was my first idea but apparently it won't work as I need because it would return only the difference between today's rates and yesterdays. And I need the getter to output one value - the difference between the days rates, kinda: this.store.recentHistory[index].rate - this.store.recentHistory[index + 1].rate

I don't quite understand how to code this, so I appreciate any advice

I'm not sure my attempt is correct, because I see the error (below)

this.store.recentHistory.reduce((prev, item, index) => {
  return prev.rate - item.rate

Argument of type '(prev: HistoryCard, item: HistoryCard, index: number) => number' is not assignable to parameter of type '(previousValue: HistoryCard, currentValue: HistoryCard, currentIndex: number, array: HistoryCard[]) => HistoryCard'. Type 'number' is not assignable to type 'HistoryCard'.

about 4 years ago · Juan Pablo Isaza
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!