I have something like this:
$scope.traveler = [
{ description: \'Senior\', Amount: 50},
{ description: \'Senior\', Amount: 50},
You can do the following:
$scope.traveler.map(o=>o.Amount).reduce((a,c)=>a+c);
It's working for me in TypeScript
and JavaScript
:
let lst = [
{ description:'Senior', price: 10},
{ description:'Adult', price: 20},
{ description:'Child', price: 30}
];
let sum = lst.map(o => o.price).reduce((a, c) => { return a + c });
console.log(sum);
I hope is useful.
I always avoid changing prototype method and adding library so this is my solution:
Using reduce Array prototype method is sufficient
// + operator for casting to Number
items.reduce((a, b) => +a + +b.price, 0);
can also use Array.prototype.forEach()
let totalAmount = 0;
$scope.traveler.forEach( data => totalAmount = totalAmount + data.Amount);
return totalAmount;