I\'ll admit I\'m weak in JavaScript and JSON. I\'ve spent a lot of time attempting to figure out why numbers from my objects returns NaN when they are added together. With t
you need to initialize jan first
var jan = 0;
here's the example - link
I like this approach. It basically sets the value to 0 on the first iteration when jan doesn't exist.
jan = (jan || 0) + parseFloat(data[i].jan);
var jan = 0; //this should solve it
for (var i=0;i<data.length;i++){
if(data[i].jan != null){
jan += parseFloat(data[i].jan);
console.log(jan);
}
}
Try this should solve it :)
Explanation as quoted by DON in comments below:
var jan; this will declare variable as undefined, so when you try to add values with undefined you will get as NaN, so the answer here with var jan = 0 will work – DON