Arrays in JavaScript/TypeScript also have these kind of methods. You can again filter with you condition and then use reduce aggregation function to sum the items.
Copyconst sum = receiptItems.filter(item => item.tax === '25.00')
.reduce((sum, current) => sum + current.total, 0);
item.tax === '25.00' - this part you must adjust with your logic
Expert Code Blog
expertcodeblog.wordpress.com โบ 2018 โบ 10 โบ 31 โบ typescript-sum-of-object-properties
TypeScript: sum of object properties within an array | Expert Code Blog
October 31, 2018 - let sum: number = 0; itemArray.forEach(a => sum += a.value); console.log(sum ); // expected output: 81 ... Like Loading... To avoid creating temporary array in object array case, use reduce directly:
javascript - Better way to sum a property value in an array
Yes if it comes after the sum function is created. 2014-12-03T10:39:40.617Z+00:00 ... This can result in NaN, if the prop does not exist (undefined) in one of the objects in the array. More on stackoverflow.com
How to sum an array of objects with a condition in typescript
Hi I am doing addition of key value from objects of an array. I want to exclude the object from the addition if it has some conditions. Example: var arrItem = [{"id": 1, "value":100},{"id": 2, "value":300},{"id": 3, "value":400}]; //addition this.sum = arrItem.map((a:any) => a.value).reduc... More on stackoverflow.com
To sum values of object array in TS/JS
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
Sum values of objects in array
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
Videos
Typescript, Array reduce examples
10 Ways to Sum Array Elements in JavaScript - YouTube
00:58
How to SUM array of object value in Js - YouTube
16:08
Three Sum (Beats 99%) - Leet Code 15 - TypeScript - YouTube
06:57
Codewars - Typescript - Sum of differences in array - YouTube
12:11
Typescript, Array reduce examples - YouTube
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
GitHub
gist.github.com โบ 4442775
TypeScript example: function Array prototype: sum ยท GitHub
TypeScript example: function Array prototype: sum. GitHub Gist: instantly share code, notes, and snippets.
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 - The initial value 0 ensures the function works correctly with empty arrays and provides a starting point for the accumulation. Each iteration adds the current number to the running total. The method returns a single value (the sum) rather than a new array, making it perfect for aggregation operations like summing.
Webdevtutor
webdevtutor.net โบ blog โบ typescript-sum-values-in-object-array
Sums of Values in Object Array with TypeScript
We use the reduce() method to iterate over the array of objects. For each object, we add its totalQuantity property value to the accumulator using the binary operator +. Finally, we return the accumulated sum as the result. These examples demonstrate how you can use TypeScript's built-in methods ...
Python Guides
pythonguides.com โบ sum-of-an-array-in-typescript
How To Calculate The Sum Of An Array In TypeScript?
January 24, 2025 - The callback function is executed for each element in the array, and the result is accumulated in the sum variable. The second argument to reduce() is the initial value of the accumulator, which is set to 0 in this case. ... In the examples above, we assumed that the array contains only numbers.
Blogger
bestwaytocode.blogspot.com โบ 2017 โบ 06 โบ typescript-foreach-sum-field-in-items.html
TypeScript: ForEach Sum Field in Items of an Array ~ Best Way to Code
June 29, 2017 - public getTotalWeight(): number { var total = 0; if (this.items != null && this.items.length > 0) { this.items.forEa...
A2-g
a2-g.com โบ hixjemo โบ typescript-sum-values-in-array-of-objects.html
Typescript sum values in array of objects
Using Array. TypeScript, like JavaScript, allows you to work with arrays of values. Itโs basically a group of related methods and properties that describe an object. length; i++){ sum += numbers[i] } console.
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