I have an object called fundsBeingSold. This object contains an Array[] of numbers named sliderValueArr.
If this array has three values..
0: 59
1: 10
2: 12
how do I set a variable to be the sum of these values?
Thanks
I have an object called fundsBeingSold. This object contains an Array[] of numbers named sliderValueArr.
If this array has three values..
0: 59
1: 10
2: 12
how do I set a variable to be the sum of these values?
Thanks
Use reduce:
var sum = fundsBeingSold.sliderValueArr.reduce(function(a,b){return a+b;},0);
You could also use JQuery for that:
$.each(fundsBeingSold.sliderValueArr,function(){sum+=parseFloat(this) || 0;});
If you want, you could also add a prototype to do it faster, like so:
Array.prototype.sum = function() {
return this.reduce(function(a,b){return a+b;});
}
And you would use it like this: var mySum = fundsBeingSold.sliderValueArr.sum();