Step: Bucket with group_by()

Bucket with group_by()

group_by(f) sorts by f and splits an array into sub-arrays of equal f — an array of groups. Pair it with map to summarize each bucket.

group_by(.category)
  | map({key: .[0].category, total: (map(.price) | add)})

Your turn: for each category, output {category, count} where count is how many products are in it.

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:
[
  {
    "category": "office",
    "count": 2
  },
  {
    "category": "tools",
    "count": 2
  }
]
Step 21 of 27