Reduce

Reduce
πŸ‘¨β€πŸ’Ό The reduce method accumulates an array into a single value. It's the most powerful (and sometimes confusing) array methodβ€”and a cornerstone of functional programming.
const numbers = [1, 2, 3, 4]
const sum = numbers.reduce((acc, n) => acc + n, 0)
// 10
Parameters:
  • accumulator (acc) - The running total
  • current (n) - The current element
  • initial value (0) - Starting point
🐨 Open
index.ts
and use the provided products fixture:
  1. Create total with reduce β€” sum of all prices, starting from 0 (expected 1579.95)
  2. Create mostExpensive with reduce β€” the product object with the highest price (expected name 'Laptop', price 999.99)
  3. Create countByCategory with reduce β€” an object of category counts (expected { Electronics: 3, Kitchen: 2 })
  4. Export total, mostExpensive, and countByCategory
πŸ’° reduce combines all items into one accumulated result.
πŸ’° When counting with an object accumulator, you can either update a single object or return a new object each step β€” choose the approach you find clearer.

Please set the playground first

Loading "Reduce"
Loading "Reduce"