David Alzate David Alzate - 1 year ago 90
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 Source

You can do this

var total = Total.map(function(object) { return object.SubTotal; })
                 .reduce(function(prevSum, nextNum) { return prevSum + nextNum; });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download