I'm getting info from an api, and what I want to do is sum the values inside of it. Let's say that the next code:
function totalPesos(){
$http.get('/api/valueForTest')
.then(function(data){
$scope.resumePesos = data.data.Response;
//console.log($scope.resumePesos);
}
Get the next answer:
[{Id: 60, Name: Chuck, Quantity: 300},
{Id: 61, Name: Arthur, Quantity: 199},
{Id: 62, Name: John, Quantity: 450}]
What I want to do is sum the Quantity. How can I do that? I've tried with:
$scope.resumePesos.reduce(function(a,b){return a + b; });
But the answer was [object Object]
aandbare objects, you need to access theQuantityproperty.$scope.resumePesos.reduce((a,b) => a.Quantity + b.Quantity);aargument is the accumulator, so it should be:a + b.Quantity.