I have this scope function where it has a cost and a formatted value in data. That cost and formatted value are coming from an API. The formatted value can be in a different currency not limited to Euro or USD, but globally, Baht, PHP, CHF, etc.
How do I perform math operations like adding using the formattedValue
data, instead of using the value data?
I want to retain the symbol or format of whatever currency I received as a response from the API. The sum will be displayed in the DOM with the currency symbol and format.
$scope.performMath = () => {
const prices = [
{
price: {
formattedValue: "€ 200,00",
value: "200"
}
},
{
price: {
formattedValue: "€ 400,00",
value: "400"
}
},
{
price: {
formattedValue: "€ 200,00",
value: "500"
}
}];
const sumPrices = (prices) => prices.reduce((value, item) => value + (parseFloat(item.price.value) || 0), 0);
$scope.totalItemPrice = sumPrices(prices).toFixed(2);
};