All Challengesintermediate
Array reduce explained
reduce is the most powerful and most misunderstood array method. AI uses it for aggregations.
arraysreduceaggregationaccumulation
javascript
const orders = [
{ product: 'Book', price: 12, qty: 2 },
{ product: 'Pen', price: 3, qty: 5 },
{ product: 'Notebook', price: 8, qty: 1 }
]
const total = orders.reduce((acc, order) => {
return acc + (order.price * order.qty)
}, 0)
const byProduct = orders.reduce((acc, order) => {
acc[order.product] = order.price * order.qty
return acc
}, {})Question
What are the values of total and byProduct?