// http://stackoverflow.com/questions/39828497/sum-of-string-values-on-array-object/
console.clear();
let array = [
{quantity: 1, amount: "24.99"},
{quantity: 5, amount: "4.99"}
]
function sumProperty(arr, type) {
return arr.reduce((total, obj) => {
if (typeof obj[type] === 'string') {
return total + Number(obj[type]);
}
return total + obj[type];
}, 0);
}
let totalAmount = ( sumProperty(array, 'amount') ).toFixed(2);
console.log( totalAmount ); // 29.98
let totalQuantity = sumProperty(array, 'quantity');
console.log( totalQuantity ); // 6