Syntax, Parameter, and Return Value
Syntax:
_.reduce(collection, [iteratee=_.identity], [accumulator])
Parameters:
-
collection (Array|Object): The collection to iterate over.
-
[iteratee=_.identity] (Function): The function invoked per iteration.
- [accumulator]: The initial value to start accumulation.
Return Value:
Returns the accumulated value.
Examples
Summing an Array of Numbers:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5];
var sum = _.reduce(numbers, (sum, n) => sum + n, 0);
console.log(sum);

You can also try this code with Online Javascript Compiler
Run Code
Output:
15
Demonstrates adding up the numbers in an array.
Concatenating String Elements:
JavaScript
var strings = ['hello', 'world', 'lodash'];
var concatenated = _.reduce(strings, (result, str) => result + ' ' + str, '');
console.log(concatenated);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'hello world lodash'
Shows concatenation of strings in an array.
Reducing to an Object:
JavaScript
var keyValuePairs = [['a', 1], ['b', 2], ['c', 3]];
var object = _.reduce(keyValuePairs, (result, [key, value]) => {
result[key] = value;
return result;
}, {});
console.log(object);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': 1, 'b': 2, 'c': 3 }
An example of transforming an array of pairs into an object.
Complex Reduction with Nested Structures:
JavaScript
var collections = [{ 'count': 5 }, { 'count': 10 }, { 'count': 15 }];
var totalCount = _.reduce(collections, (sum, obj) => sum + obj.count, 0);
console.log(totalCount);

You can also try this code with Online Javascript Compiler
Run Code
Output:
30
Demonstrates summing values from a collection of objects.
Frequently Asked Questions
How does _.reduce() differ from native JavaScript Array.reduce()?
Lodash's _.reduce() works with arrays, objects, and even strings, providing more flexibility than the native Array.reduce(), which is limited to arrays.
What happens if the accumulator is not provided in _.reduce()?
If an accumulator is not provided, _.reduce() uses the first element of the collection as the initial accumulator and starts iteration from the second element.
Is _.reduce() efficient for large collections?
_.reduce() is efficient for most collections, but its performance depends on the complexity of the iteratee function and the size of the collection.
Conclusion
Lodash's _.reduce() method is a powerful tool for accumulating values from a collection. It provides a streamlined and expressive way to iterate over a collection and combine its elements into a single result, making it indispensable for various data transformation tasks.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.