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:
  1. Use reduce to sum all prices
  2. Use reduce to find the most expensive product
  3. Use reduce to count products by category
πŸ’° reduce combines all items into one accumulated result.
Each iteration returns a new object instead of mutating the accumulator. This is the functional approachβ€”easier to reason about and debug.

Please set the playground first

Loading "Reduce"
Loading "Reduce"
Login to get access to the exclusive discord channel.
Loading Discord Posts