I know that this question has an accepted answer but I thought I'd chip in with an alternative which uses array.reduce, seeing that summing an array is the canonical example for reduce:
Copy$scope.sum = function(items, prop){
return items.reduce( function(a, b){
return a + b[prop];
}, 0);
};
$scope.travelerTotal = $scope.sum($scope.traveler, 'Amount');
Fiddle
Answer from Gruff Bunny on Stack Overflow Top answer 1 of 16
325
I know that this question has an accepted answer but I thought I'd chip in with an alternative which uses array.reduce, seeing that summing an array is the canonical example for reduce:
Copy$scope.sum = function(items, prop){
return items.reduce( function(a, b){
return a + b[prop];
}, 0);
};
$scope.travelerTotal = $scope.sum($scope.traveler, 'Amount');
Fiddle
2 of 16
286
Use reduce with destructuring to sum Amount:
Copyconst traveler = [
{ description: 'Senior', Amount: 50 },
{ description: 'Senior', Amount: 50 },
{ description: 'Adult', Amount: 75 },
{ description: 'Child', Amount: 35 },
{ description: 'Infant', Amount: 25 },
];
console.log(traveler.reduce((n, {Amount}) => n + Amount, 0));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reduce
Array.prototype.reduce() - JavaScript - MDN Web Docs
July 20, 2025 - This convention propagates to JavaScript's reduce(): you should use spreading or other copying methods where possible to create new arrays and objects as the accumulator, rather than mutating the existing one. If you decided to mutate the accumulator instead of copying it, remember to still return the modified object in the callback, or the next iteration will receive undefined.
javascript - Sum values of objects in array - Stack Overflow
Assuming you already have a sum function for simple arrays of numbers, you can implement a function that will sum the totals of all keys over an array of objects in just two expressions. No loops, hardcoded property names, anonymous functions or if/else required. More on stackoverflow.com
How do you calculate the sum of values in an array of objects?
This is what `reduce()` is for, processing an array down to a single value. You'll want something like this: const sum = products.reduce((total, product) => {return total + product.price - product.discount}, 0) More on reddit.com
Define a sum function that accepts an array of numbers OR an array of bigints
What type signature have you tried? I've been out of TS for a bit, but I believe this'll work if you make your function generic, where your argument arr: T[] and T extends number | bigint More on reddit.com
How to reduce and sum a nested object array?
// reduce product to sum via matching keys const productReducer = (sumObj, product) => { const current = sumObj[product.productName] || 0; return { ...sumObj, [product.productName]: current + product.total } } // reduce product, return [key, product] const hourMapper = ([key, hours]) => { return [key, hours.reduce(productReducer, {})]; } // restore [key, val] array to object const flattenKeyVal = (obj, keyVal) => { return { ...obj, [keyVal[0]]: keyVal[1] } } // obj -> [key,product] -> [key, reducedProduct] -> reducedObj const dayMapper = day => { return Object.entries(day).map(hourMapper).reduce(flattenKeyVal); } // put it all together for the days const mapDays = days => days.map(dayMapper); I believe this should work! Fun little challenge. Let me know if you have any questions. More on reddit.com
Videos
01:33
Javascript array sum by object property - YouTube
10 Ways to Sum Array Elements in JavaScript - YouTube
How to Find Sum of An Array of Numbers in Javascript - YouTube
How to Find Sum of An Array of Numbers in Javascript
00:58
How to SUM array of object value in Js - YouTube
ReqBin
reqbin.com › code › javascript › m81eb1ms › javascript-sum-array-example
How to get a sum of array elements in JavaScript?
November 24, 2023 - The sum of the array elements will be returned as the result of the array.reduce() method. Alternatively, you can find the sum of array elements using a "for" loop. In this JavaScript Array Sum Example, we use the reduce() method to get the ...
Sling Academy
slingacademy.com › article › javascript-ways-to-calculate-the-sum-of-an-array
JavaScript: 6 Ways to Calculate the Sum of an Array - Sling Academy
February 19, 2023 - Array.map() method is new in ES6 and beyond. This one is very useful when you have to deal with an array, including finding the sum of its elements.
sebhastian
sebhastian.com › javascript-sum-array-objects
JavaScript code recipe: sum an array of objects | sebhastian
January 18, 2021 - When you call the reduce method on array of objects, you need to always specify the initial value to prevent reduce from using the first object as the initial value. If the cart has quantity data, you can multiply the quantity by price before you add to the sum to the accumulator: let cart = [ { name: "JavaScript book", quantity: 3, price: 4, }, { name: "UGG Women's Hazel Ankle Boot", quantity: 2, price: 79, }, { name: "OXO Good Grips 11-Inch Balloon Whisk", quantity: 5, price: 9, }, ]; // totalPrice is 215 let totalPrice = cart.reduce(function (accumulator, item) { return accumulator + item.quantity * item.price; }, 0);
TutorialsPoint
tutorialspoint.com › article › sum-of-array-object-property-values-in-new-array-of-objects-in-javascript
Sum of array object property values in new array of objects in JavaScript
March 15, 2026 - Sum values: If it exists, add the numeric values to the existing entry; otherwise, create a new entry · Type conversion: Use the unary plus operator (+) to convert string numbers to integers · The +marks syntax converts string values to numbers ...
freeCodeCamp
freecodecamp.org › news › how-to-add-numbers-in-javascript-arrays
JS Sum of an Array – How to Add the Numbers in a JavaScript Array
March 31, 2023 - An array in JavaScript is an object that allows you to store an ordered collection of multiple values under a single variable name and manipulate those values in numerous ways. In this article, you will learn how to calculate the sum of all the numbers in a given array using a few different approaches.
JavaScript in Plain English
javascript.plainenglish.io › how-to-find-the-sum-of-an-array-of-objects-in-javascript-24965d883bd0
How To Find The Sum of an Array of Objects in JavaScript | by Sam C. Tomasi | JavaScript in Plain English
November 20, 2022 - In a nutshell, it’s about reducing an array of values to a single number. Ok, maybe it is better to give an example. This is a child’s card: Each child has a matched list of actions, some good, some not. Each action corresponds to a value. The sum of… ... New JavaScript and Web Development content every day.
YouTube
youtube.com › tuts make
javascript sum array of objects value - YouTube
JavaScript provides several methods to manipulate array of objects, and sum of values array of objects by key can be achieved using different approaches. In ...
Published December 18, 2023 Views 118
CoreUI
coreui.io › answers › how-to-sum-an-array-of-numbers-in-javascript
How to sum an array of numbers in JavaScript · CoreUI
September 29, 2025 - With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented array summation in components like data tables, chart calculations, and financial widgets where accurate total calculations are essential for user interfaces and business logic. From my extensive expertise, the most elegant and functional approach is using the reduce() method with an accumulator. This method is concise, readable, and follows functional programming principles while handling empty arrays gracefully.
Mish Ushakov
mish.co › posts › sum-and-average-array-of-objects-in-js
Sum and average Array of Objects in JavaScript - Mish Ushakov
October 23, 2021 - const result = sample.reduce((previous, current, index, array) => { Object.keys(current).forEach(key => { current[key] += previous[key] if (index === array.length - 1) current[key] /= array.length }) return current })
Top answer 1 of 9
37
Use
Array.prototype.reduce(), the reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
var array = [{
"adults": 2,
"children": 3
}, {
"adults": 2,
"children": 1
}];
var val = array.reduce(function(previousValue, currentValue) {
return {
adults: previousValue.adults + currentValue.adults,
children: previousValue.children + currentValue.children
}
});
console.log(val);
2 of 9
33
var array = [{"adults":2,"children":3},{"adults":2,"children":1}];
var totalChild = array.reduce((accum,item) => accum + item.children, 0)
console.log(totalChild) //output 4
Educative
educative.io › answers › how-to-get-the-sum-of-an-array-in-javascript
How to get the sum of an array in JavaScript
In this method, you iterate and add each item until you reach the last item. function sumArray(array){ let sum = 0 // the sum is initialed to 0 /* js arrays are zero-index based ourArray.length = 5, the initialization block is set to 0.
TutorialsPoint
tutorialspoint.com › sum-similar-numeric-values-within-array-of-objects-javascript
Sum similar numeric values within array of objects - JavaScript
We will then add the value property to one object and delete the other object from the array. This will be done until we reach the end of the array. On reaching, we would have reduced our array to the desired array. ... const arr = [ {"firstName":"John", "value": 89}, {"firstName":"Peter", "value": 151}, {"firstName":"Anna", "value": 200}, {"firstName":"Peter", "value": 22}, {"firstName":"Anna","value": 60} ]; const sumSimilar = arr => { const res = []; for(let i = 0; i < arr.length; i++){ const ind = res.findIndex(el => el.firstName === arr[i].firstName); if(ind === -1){ res.push(arr[i]); }else{ res[ind].value += arr[i].value; }; }; return res; }; console.log(sumSimilar(arr));