Step: Fold with reduce

Fold with reduce

reduce accumulates a stream into one value:

reduce <stream> as $item (<init>; <update>)

It starts from <init> and runs <update> for each $item, with . holding the accumulator.

reduce .[] as $x (0; . + $x.price)   # sum of prices, from scratch

Why it matters: reduce expresses aggregations add/map can't — running totals, custom merges, building an object as you go.

Your turn: sum the product prices with reduce (same total as add, but built by hand).

Input:
[
  {
    "name": "Widget",
    "price": 25,
    "category": "tools"
  },
  {
    "name": "Gadget",
    "price": 80,
    "category": "tools"
  },
  {
    "name": "Notebook",
    "price": 12,
    "category": "office"
  },
  {
    "name": "Pen",
    "price": 3,
    "category": "office"
  }
]
Expected:
120
Step 24 of 27