In this post, we will see how to resolve React — Why does this line append rather than add
Question:I have other ways of doing the same thing .. So I am not looking for a different solution to this problem … I am looking for an explanation as to why if I have defined a integer, it still concatenates with
.mapas if it were a string.
I have a basic set of data retrieved from an API:
Is this just an issue just using
.map? Can shortcut math functions be used here?
Here is my Reproducible Example
Best Answer:Your current approach using
Array#mapcreates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use
Numberfunction, or the unary plus operator to convert each string to a number.
If you have better answer, please add a comment about this, thank you!