I am currently pulling information from an API and I'm looking to perform a calculation on the data. Specifically, I want to multiply two different values from the response and then sum up the totals. While I already know how to sum all the values using reduce:
function getHistoricSales(){
$http.get('api/SomeApi')
.then(function(data){
$scope.salesResult = data.data.Response;
var hResults = $scope.salesResult.reduce((a, b) => a + b.Cost, 0);
$scope.historic = hResult.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
});
}
However, if for example, the response includes not just the Cost (b.Cost)
, but also the Quantity. So, my question is how can I first multiply each Cost
by its corresponding Quantity
, and then sum the results?
I am working with JavaScript and AngularJS.
Any help would be greatly appreciated. Thank you in advance...