-1

I need to compute sum of two values hat placed in my data. But my code working like not summ, but as strings concatination.

How to get it's working with values as with ints:

computed: {
  myvalue: function () {
    return this.my_dates[0]['2018-03-23']['april'] + this.my_dates[0]['2018-03-23']['may']
    }
}

My data property like:

  data: {
    my_dates:
    [
        {
          "2018-03-23": {
            "april": 10,
            "may": 9,
             ...
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • Possible duplicate of [How to force JS to do math instead of putting two strings together](https://stackoverflow.com/questions/4841373/how-to-force-js-to-do-math-instead-of-putting-two-strings-together) and [many, many others](https://stackoverflow.com/search?q=javascript+concatenates+instead+of+sum) – JJJ Jul 10 '18 at 16:03

1 Answers1

1

you can convert the two (strings?) to numbers like this

computed: {
  myvalue: function () {
    return Number(this.my_dates[0]['2018-03-23']['april']) + Number(this.my_dates[0]['2018-03-23']['may'])
    }
}

Depending on the input and expected output, you can also use parseFloat() or parseInt instead of Number()

Daniel
  • 34,125
  • 17
  • 102
  • 150