This post is available in french here
The reduce()
method is a powerful JavaScript feature that reduces an array to a single value by applying an operation to each element of the array.
If you're an advanced JavaScript developer, you've probably come across this method before.
In this article, we'll take a deep dive into how it works, examining its fundamental aspects and exploring advanced use cases.
Method signature
The reduce()
method has the following signature:
array.reduce(callback(accumulator, currentvalue, index, array), initialValue);
-
callback
: A function that is called for each element in the array. This function takes four arguments: -
accumulator
: The accumulated value so far. -
currentvalue
: The value of the current element in the array. -
index
: The index of the current element in the array (optional). -
array
: The array on whichreduce()
is called (optional). -
initialValue
: An optional initial value which is used as the accumulator's initial value on the first iteration. If this value is not supplied, the first element of the array will be used as the initial value and the loop will start from the second element.
How it works
- The
callback
function is called for each element of the array, starting from the first element (or with the initial value if supplied). - Each time the
callback
function is called, the accumulator is updated according to the callback result. The accumulator stores the value accumulated so far. - The loop continues until all array elements have been processed.
- At the end of the iteration, the
reduce()
method returns the final value of the accumulator.
Basic example
Here's the simplest example of how to use reduce()
: calculate the sum of an array's elements:
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, current) => acc + current, 0);
console.log(sum); // 15
In this example, we've provided a callback function that adds the current value to the accumulator at each iteration, starting with an initial value of 0.
Use case
The reduce()
method in JavaScript is very versatile and can be used for a variety of tasks. Here are a few examples of how reduce()
can be used in different contexts:
Grouping elements of an array
You can use reduce()
to group elements of an array according to certain properties. For example, suppose you have an array of objects representing students and you want to group them by class.
const students = [
{ name: 'Alice', class: 'A' },
{ name: 'Bob', class: 'B' },
{ name: 'Charlie', class: 'A' },
// ...
];
const groupedByClass = students.reduce((acc, current) => {
const className = current.class;
if (!acc[className]) acc[className] = [];
acc[className].push(current);
return acc;
}, {});
console.log(groupedByClass);
// {
// A: [{ name: "Alice", class: "A" }, { name: "Charlie", class: "A" }],
// B: [{ name: "Bob", class: "B" }]
// }
Calculating statistics
You can use reduce()
to calculate statistics from data. For example, to find the mean and standard deviation of a set of numerical data.
const data = [10, 20, 30, 40, 50];
const stats = data.reduce(
(acc, current) => {
acc.sum += current;
acc.squareSum += current * current;
return acc;
},
{ sum: 0, squareSum: 0 }
);
const mean = stats.sum / data.length;
const variance = stats.squareSum / data.length - mean * mean;
const stdDeviation = Math.sqrt(variance);
console.log(mean); // Moyenne
console.log(stdDeviation); // Écart-type
Building an object from an array
You can use reduce()
to build an object from an array, using the array elements as keys or values.
const pairs = [
['a', 1],
['b', 2],
['c', 3],
];
const obj = pairs.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
console.log(obj); //{ "a": 1, "b": 2, "c": 3 }
Advanced use cases
Find the longest sequence of identical characters
Use reduce()
to find the longest sequence of identical characters in a string.
const text = 'aaabbccccccdddddddd';
const longestSequence = text.split('').reduce(
(acc, current) => {
if (current === acc.currentChar) {
acc.currentCount++;
if (acc.currentCount > acc.maxCount) {
acc.maxCount = acc.currentCount;
acc.maxLength = acc.currentCount * current.length;
}
} else {
acc.currentChar = current;
acc.currentCount = 1;
}
return acc;
},
{ currentChar: '', currentCount: 0, maxCount: 0, maxLength: 0 }
);
console.log(longestSequence);
// { currentChar: "d", currentCount: 8, maxCount: 8, maxLength: 8 }
Managing complex database queries
Using reduce()
, you can manage complex queries and dynamically generate SQL queries based on filters and conditions.
const filters = [
{ field: 'age', operator: '>', value: 30 },
{ field: 'country', operator: '=', value: 'USA' },
// ...
];
const query = filters.reduce((acc, filter, index) => {
const condition = `${filter.field} ${filter.operator} "${filter.value}"`;
if (index === 0) {
return `SELECT * FROM table WHERE ${condition}`;
} else {
return `${acc} AND ${condition}`;
}
}, '');
console.log(query);
// SELECT * FROM table WHERE age > "30" AND country = "USA"
Building a pipe
Building a pipe (or chain of functions) from the reduce()
method is an elegant way of applying a series of functions to an input value, where the output of each function becomes the input of the next. This can be useful for organizing and executing operations sequentially.
const users = [
{ id: 1, name: 'Alice', age: 28, country: 'USA' },
{ id: 2, name: 'Bob', age: 35, country: 'Canada' },
{ id: 3, name: 'Charlie', age: 22, country: 'USA' },
{ id: 4, name: 'David', age: 40, country: 'UK' },
{ id: 5, name: 'Eve', age: 30, country: 'Canada' },
];
const filters = [
(users) => users.filter((user) => user.age >= 30),
(users) => users.filter((user) => user.country === 'USA'),
];
const filteredUsers = filters.reduce((acc, filter) => filter(acc), users);
console.log(filteredUsers);
// [{ id: 1, name: "Alice", age: 28, country: "USA" }]
In this example, we have a series of filters stored in a filters
array. Each filter is a function that takes an array of users as input and returns a filtered subset of these users according to certain criteria.
We then use reduce()
to sequentially apply each filter from the initial users
value. At each iteration, the current filter is applied to the output of the previous filter, and so on. The final result (filteredUsers
) is the subset of users who meet all the filter criteria.
Common mistakes and best practices
When using reduce()
, there are a few common mistakes to avoid. Always provide an initial value if necessary, otherwise the first element of the array will be used as the initial value.
Keep your code readable by choosing explicit variable names for the accumulator and current value.
If you have simpler use cases that can be solved with map()
, filter()
, or other methods, use these instead, as they are often more readable and efficient for simple operations.
If your use of reduce()
is complex, comment your code to clearly explain what the callback function does. This makes it easier for you and others to understand when you re-read the code.
Conclusion
The reduce()
method is extremely versatile and can be used to perform operations such as transforming, filtering, grouping data, calculating statistics and so on. It is often used to solve complex data processing problems.
It may seem difficult to understand at first, but it's a powerful and essential method in JavaSCript.
Top comments (0)