Feb 01 2025
If you are a javascript developer you have probably heard of reduce functions. If you don't know what it is and why we need it. Don't worry I will sort this for you with 3 examples.
#1. Calculating total reveneu
Let's say you have a revenue of different products [100, 240, 1400, 400, 50] and you are ready to calculate total revenue. In order to do that you kinda need to go all over the values and add them to a variable called totalReveneu. That’s how we do it in a for loop. But using a reduce function you can make your job a lot easier.
const revenue = [100,240,1400,400,50]
const sum = revenue.reduce((accumulator, num) => accumulator + num, 0)
console.log(sum) // Output: 2190As you can see, the code is way smaller compared to a traditional for-loop. The .reduce() function takes the first input as a callback function with two arguments and initializer number 0.
Here is what the opperation looks like
Okay that's easy right? Let's pick another example when you need to group an array based on a specific conditions. I pick age for simplicty here.
#2. Grouping Data
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 25 },
{ name: 'David', age: 30 },
{ name: 'Eve', age: 35 },
];
const groupedAge = people.reduce((accumulator, person) => {
// Determine the age group (e.g., 20s, 30s, etc.)
const ageGroup = Math.floor(person.age / 10) * 10} + 's';
// If the age group doesn't exist in the accumulator, create it.
// This prevents throwing an error when we try push in to it.
if (!acc[ageGroup]) {
acc[ageGroup] = [];
}
// Add the person to the appropriate age group
acc[ageGroup].push(person);
return acc;
}, {})
console.log(groupedByAge);Now we take a bit further and expalnd the callback function. We determine ageGroup (20s, 30s) and create that group if it doesn't exist and push it to the appropriate group. Remmember we always need the updated version of `acc` so at the end of our callback function we have `return acc;`
Note: we are not initializing accumulator as `0` this time instead as an empty object {} . Since we are returning and object and altering it in every loop.
The output looks something like
{
'20s': [ { name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 } ],
'30s': [
{ name: 'Bob', age: 30 },
{ name: 'David', age: 30 },
{ name: 'Eve', age: 35 }
]
}#3. Summarizing Sales Data by Category
| Id | Product | Quantity | Price | Category |
|---|---|---|---|---|
| 1 | Phone | 2 | 500 | Electronics |
| 2 | TV | 1 | 1000 | Electronics |
| 3 | T-Shirt | 5 | 20 | Clothing |
| 4 | Jeans | 2 | 40 | Clothing |
| 5 | Laptop | 1 | 1500 | Electronics |
| 6 | Tablet | 3 | 300 | Electronics |
| 7 | Jacket | 1 | 120 | Clothing |
| 8 | Smartwatch | 4 | 200 | Electronics |
| 9 | Sneakers | 2 | 80 | Clothing |
| 10 | Gaming Console | 1 | 400 | Electronics |
| 11 | Socks | 10 | 5 | Clothing |
| 12 | Headphones | 3 | 150 | Electronics |
| 13 | Dress | 2 | 60 | Clothing |
| 14 | Camera | 1 | 800 | Electronics |
| 15 | Hat | 3 | 25 | Clothing |
| 16 | Monitor | 2 | 350 | Electronics |
| 17 | Sweater | 4 | 50 | Clothing |
| 18 | Mouse | 5 | 30 | Electronics |
| 19 | Keyboard | 3 | 60 | Electronics |
| 20 | Scarf | 6 | 15 | Clothing |