Ad

NaN When Trying To Multiply A Json Value With Local Variable

- 1 answer

i'm trying to change a JSON value from an object from seconds to hours. The problem is, when I try to multiply it with my local constant to convert it, it returns a NaN value.

i've already tried using JSON.stringify(Json_value) and it didn't help

const secToHours = '0,000277778';
const hourTime = secToHours*JSON.stringify(jsonData.duration);
logs = []
logs.push({"id":jsonData.id,"key":jsonData.issueKey, "date": dateFormatted, "duration": hourTime})
                var array = [{user:jsonData.userId, logs:logs}];
                console.log(array[0]);

This is the console output:
{
  user: 'XXXXX',
  logs: [ { id: XXXXX, key: 'XX-X', date: '2019-7-3', duration: NaN } ]
}

I don't know why it's returning a NaN value for the multiplication, and if i print the values separated, they return the respective const value and the json data.

Ad

Answer

0,000277778 is not a valid JS number - your local convention may use comma's as a decimal point, but JS expects a dot.

You don't need/want to use JSON.stringify on jsonData.duration though I can't see from this if you need to manipulate it in some other way - where is it declared/what value does it have on line 2?

Ad
source: stackoverflow.com
Ad