Functional Programming in JS - 3
Using reduce in array
by sendil J
HTML
<textarea id="output" style="width:100vh;height:50vh;" readonly></textarea>
JavaScript
var orders = [
{price: 150},
{price: 50},
{price: 250},
{price: 550},
{price: 450}
]
/*var totalAmount = orders.reduce(function(sum, order){
return sum + order.price
}, 0)*/ // sum is initiated with 0
// shorthand method -
var totalAmount = orders.reduce((sum, order) => sum + order.price, 0)
var result = document.getElementById('output');
result.innerHTML = totalAmount;