David Alzate David Alzate - 4 months ago 42
Javascript Question

Sum values from array Javascript

I have defined a variable called Total which is a Array like this:

Var Total[{"SubTotal":9000},{"SubTotal":20000},{"SubTotal":14000}]

I want to sum values 9000, 20000, 14000


You can do this

var total = Total.map(function(object) { return object.SubTotal; })
                 .reduce(function(prevSum, nextNum) { return prevSum + nextNum; });