Sum returns NAN even when converting the number to angular

Asked

Viewed 236 times

0

I have a repeating structure that should add some values:

The variable qtdeEstoque was defined as number and the variable variacaoForm.value.variacoes.estoque_variacao is a number, but when I try to add it this way instead of increasing the value it puts in front, for example: number 10 and 5, he joins and gets 105:

  for(let i=0;i<this.variacaoForm.value.variacoes.length;i++){
       this.qtdeEstoque+= this.variacaoForm.value.variacoes[i].estoque_variacao
  }
  console.log(this.qtdeEstoque);

I tried some other ways using toInt() and Number():

  for(let i=0;i<this.variacaoForm.value.variacoes.length;i++){
    this.qtdeEstoque+= Number(this.variacaoForm.value.variacoes[i].estoque_variacao)
  }
  console.log(this.qtdeEstoque);

But he returns NAN, how can I fix this?

  • Do you have an example of data for testing? Ex: 15.45

  • the values are int, as specified in the post, if you put 10 and 5 it concatenates and gets 105, and using toInt() and Number() returns NAN

  • How did you define this property?

  • 1

    pq she took down vote?

1 answer

2


App

this.qtdeEstoque = 0;  
for(let i=0;i<this.variacaoForm.value.variacoes.length;i++){
       this.qtdeEstoque+= this.variacaoForm.value.variacoes[i].estoque_variacao
  }
  console.log(this.qtdeEstoque);

tips:

when adding the value public qtdeEstoque: number = 0 you can get ahead at once

  • It worked, I needed to initialize this variable with value 0.

  • 1

    this :number and redundant if you initialize with 0 typescript already determines the type for number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.