![]() |
VOOZH | about |
Lodash's _.reduce() method iterates over a collection, applying an iterative function to each element to accumulate a single value. The method starts with an initial value or the first element if no accumulator is provided, progressively reducing the collection.
Syntax
_.reduce(collection, iteratee, accumulator)Parameters:
This method accepts three parameters as mentioned above and described below:
Return Value: This method returns the accumulated value.
Example 1: In this example we use Lodash's _.reduce() method to sum the elements in the users array, starting from 0. The result is 10.
Output:
10Example 2: In this example we use Lodash's _.reduce() method to group object keys (users) by their values.
Output:
{ '2': ['p', 'r', 's'], '3': ['q'] }Note:
This code will not work in normal JavaScript because it requires the library lodash to be installed.