This’d be exactly the job for reduce
.
If you’re using ECMAScript 2015 (aka ECMAScript 6):
const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0);
console.log(sum); // 6
For older JS:
const sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty
function add(accumulator, a) {
return accumulator + a;
}
console.log(sum); // 6
Isn’t that pretty? 🙂
Related Contents:
- Remove duplicate values from JS array [duplicate]
- Check if an array is empty or exists
- Get the index of the object inside an array, matching a condition
- JQuery .each() backwards
- Convert array to JSON
- Converting a JS object to an array using jQuery
- How to add an object to an array
- How to change value of object which is inside an array using JavaScript or jQuery?
- How to set DOM element as first child?
- Why does typeof array with objects return “object” and not “array”? [duplicate]
- console.log(result) prints [object Object]. How do I get result.name? [duplicate]
- Converting JavaScript object with numeric keys into array
- How to get a number of random elements from an array?
- Convert javascript array to string
- remove first element from array and return the array minus the first element
- How to find index of all occurrences of element in array?
- How to split a long array into smaller arrays, with JavaScript
- javascript, is there an isObject function like isArray? [duplicate]
- javascript filter array of objects
- Using jQuery to compare two arrays of Javascript objects
- Javascript algorithm to find elements in array that are not in another array
- How to find a value in an array of objects in JavaScript?
- How to check if an array is empty or exists?
- What is the most efficient way to sort an Html Select’s Options by value, while preserving the currently selected item?
- break array of objects into separate arrays based on a property
- get the second to last item of an array?
- how do I create an array in jquery?
- store and retrieve javascript arrays into and from HTML5 data attributes
- Cloning an object in JavaScript
- Finding matches between multiple JavaScript Arrays
- Adding items to an object through the .push() method
- Store and retrieve JavaScript arrays into and from HTML data attributes
- Filter array of objects with another array of objects
- How to Compare two Arrays are Equal using Javascript? [duplicate]
- How to get an Array with jQuery, multiple with the same name
- How to check whether multiple values exist within an Javascript array
- Is there a method to clone an array in jQuery?
- Remove items from array with splice in for loop [duplicate]
- jQuery appending an array of elements
- How to flatten array in jQuery?
- how to prevent adding duplicate keys to a javascript array
- jQuery min/max property from array of elements
- Passing (laravel) Array in Javascript
- Convert simple array into two-dimensional array (matrix)
- Sorting an Array of JavaScript Objects a Specific Order (using existing function)
- How can I remove empty object in from an array in JS
- How can I sort a javascript array of objects numerically and then alphabetically? [duplicate]
- Array functions in jQuery
- What is the most efficient way to get the first item from an associative array in JavaScript?
- Parsing JSON objects for HTML table