Skip to content Skip to sidebar Skip to footer

Summing Numbers Stored In An Array In Javascript

I want to sum a list of numbers stored in a JavaScript object. The object is created and updated using this code: var myscore = $('input[name='Points1']').val(); scorelist = JSON.p

Solution 1:

If you push() to scorelist, I'd be tempted to say it's likely an Array.

You could use reduce().

var total = scorelist.reduce(function(total, score) {
    return total + +score;
}, 0);

Post a Comment for "Summing Numbers Stored In An Array In Javascript"