David Alzate David Alzate - 27 days ago 6
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

Answer

You can do this

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