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 OverflowI 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
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
javascript sum array of arrays of objects together - Stack Overflow
javascript - Sum values of objects in array - Stack Overflow
How do you calculate the sum of values in an array of objects?
Define a sum function that accepts an array of numbers OR an array of bigints
Videos
Ecmascript5 solution:
var arr = [
[{year:2015, value:23000, value1:1000},{year:2016,value:1000, value1:2000},{year:2017,value:400, value1:3000}],
[{year:2015, value:10000, value1:1000},{year:2016,value:2000, value1:3000},{year:2017,value:500, value1:2000}]
],
// summing up values for each `year`
years = arr.reduce(function (r, a) {
a.forEach(function (o) {
y = o.year;
if (Array.isArray(r[y])) {
r[y][0] += o.value;
r[y][1] += o.value1;
} else {
r[y] = [o.value, o.value1];
}
});
return r;
}, {}),
// constructing the resulting array containing objects with `year` totals
result = Object.keys(years).map(function (y) {
return {year: y, value: years[y][0], value1: years[y][1]};
});
console.log(result);
You can loop over the items and add value agains a year number (key) in a map / object.
var data = [
[{year:2015, value:23000},{year:2016,value:1000},{year:2017,value:400}],
[{year:2015, value:10000},{year:2016,value:2000},{year:2017,value:500}],
];
var output = {};
data.forEach(function(item) {
item.forEach(function(obj) {
output[obj.year] = (output[obj.year] || 0) + obj.value;
});
});
output = [output]
console.log(output);
One way is to flat this 2D array first.
var output = {};
var flatArray = data.reduce(function(a, b) {
return a.concat(b);
}, []);
flatArray.forEach(function(obj) {
output[obj.year] = (output[obj.year] || 0) + obj.value;
});
output = [output]
console.log(output);
output: [{ 2015: 33000, 2016: 3000, 2017: 900 }]
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);
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