-1

I am building array of values which will be typed into input field. Array looks like this example:

[{mykey:"32"},{mykey:"32"}]

I tried to calculate sum of it with this example:

reduce(function(prev2, cur) {
      return prev2 + cur.mykey;
    }, 0);

But it only put numbers behind. And don`t calculate. Maybe my array should look like this to calculate? How to get right input as number instead of string?

[{mykey:32},{mykey:32}]
SaschaK
  • 170
  • 2
  • 15

1 Answers1

1

You need to convert string to a number before adding. Number - MDN documentation

reduce(function(prev2, cur) {
    return prev2 + Number(cur.mykey);
}, 0);
Prabh
  • 989
  • 5
  • 10