Aggregation turns many items into fewer, summarized items. Common uses include generating reports, calculating totals, and grouping data by category.
intermediate
Chapter 5: Data Transformation & Manipulation3. Aggregation & Summarization
Combine multiple items into summaries.
15m Lesson 3 of 5
Code Examples
Group By and Summarize
javascript
const items = $input.all().map(i => i.json);
// Group orders by product category
const grouped = items.reduce((acc, order) => {
const cat = order.category;
if (!acc[cat]) acc[cat] = { count: 0, revenue: 0 };
acc[cat].count++;
acc[cat].revenue += order.amount;
return acc;
}, {});
// Convert to array of items
return Object.entries(grouped).map(([category, stats]) => ({
json: {
category,
orderCount: stats.count,
totalRevenue: stats.revenue,
avgOrderValue: (stats.revenue / stats.count).toFixed(2)
}
}));Pro Tips
- 💡Use the Summarize node (built-in) for simple aggregations before reaching for the Code node
Comprehension Quiz
Answer 2 of 2 correctly to unlock lesson completion.
1. What does aggregation do?
2. Which built-in node handles simple aggregation?
Pass the quiz above to unlock lesson completion