You should be able to use reduce
.
var sum = array.reduce(function(pv, cv) { return pv + cv; }, 0);
And with arrow functions introduced in ES6, it’s even simpler:
sum = array.reduce((pv, cv) => pv + cv, 0);
More Related Contents:
- How can I create an option element using javascript and set it’s value [duplicate]
- Javascript call() & apply() vs bind()?
- Passing an array as a function parameter in JavaScript
- How to add an object to an array
- How can I create every combination possible for the contents of two arrays?
- Javascript Array of Functions
- Why doesn’t console.log work when passed as a parameter to forEach?
- How can I use optional chaining with arrays and functions?
- Find all values by specific key in a deep nested object
- .indexOf function on an array not working in IE7/8 using JavaScript
- Short circuit Array.forEach like calling break
- Filter array of objects based on another array in javascript
- Check if an array contains any element of another array in JavaScript
- MongoDB, remove object from array
- How to find first element of array matching a boolean condition in JavaScript?
- How to loop through all the elements returned from getElementsByTagName [duplicate]
- How to compare two objects and get key-value pairs of their differences?
- Sort an array of object by a property (with custom order, not alphabetically)
- What’s the best way to loop through a set of elements in JavaScript?
- How to sort an associative array by its values in Javascript?
- Javascript: Call a function after specific time period
- anchor jumping by using javascript
- How to merge multiple array of object by ID in javascript?
- How to convert an array of key-value tuples into an object
- Is there a JavaScript function that reduces a fraction
- Sort an array of objects by multiple properties
- Using setInterval() with a .map method in array and returning as a promise
- How to create an array of object literals in a loop?
- Populating JavaScript Array from JSP List
- How are JavaScript arrays implemented?