Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda