Lodash reduce functionality for transforming arrays and objects by applying an iteratee function to accumulate results
npx @tessl/cli install tessl/npm-lodash--reduce@4.6.0Lodash reduce functionality provides enhanced collection reduction capabilities for both arrays and objects. It transforms collections by applying an iteratee function to accumulate results, offering improved performance, consistent behavior across data types, and powerful iteratee shorthand support beyond native JavaScript reduce.
npm install lodash// Full lodash import
const _ = require('lodash');ES6 Import:
import _ from 'lodash';Standalone package (if available):
const reduce = require('lodash.reduce');const _ = require('lodash');
// Array reduction with initial accumulator
const sum = _.reduce([1, 2, 3, 4], function(total, n) {
return total + n;
}, 0);
// => 10
// Object reduction without initial accumulator
const flattened = _.reduce([[0, 1], [2, 3], [4, 5]], function(flat, item) {
return flat.concat(item);
});
// => [0, 1, 2, 3, 4, 5]
// Object transformation and grouping
const grouped = _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
return result;
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }Reduces collections to a single value by applying an iteratee function that accumulates results through each element.
/**
* Reduces collection to a value which is the accumulated result of running
* each element through iteratee, where each successive invocation is supplied
* the return value of the previous. If accumulator is not given, the first
* element of collection is used as the initial value.
*
* @param {Array|Object} collection - The collection to iterate over
* @param {Function} [iteratee=_.identity] - The function invoked per iteration
* @param {*} [accumulator] - The initial value
* @returns {*} Returns the accumulated value
*/
_.reduce(collection, iteratee, accumulator);Iteratee Arguments:
accumulator - The accumulated valuevalue - The current element valueindex|key - The current element index (arrays) or key (objects)collection - The collection being iteratedSupported Collection Types:
Similar to reduce but iterates over elements from right to left, useful for operations where order matters.
/**
* This method is like _.reduce except that it iterates over elements of
* collection from right to left.
*
* @param {Array|Object} collection - The collection to iterate over
* @param {Function} [iteratee=_.identity] - The function invoked per iteration
* @param {*} [accumulator] - The initial value
* @returns {*} Returns the accumulated value
*/
_.reduceRight(collection, iteratee, accumulator);Usage Examples:
// Right-to-left array processing
const reversed = _.reduceRight([1, 2, 3, 4], function(accumulator, value) {
return accumulator.concat(value);
}, []);
// => [4, 3, 2, 1]
// Right-to-left object processing
const rightToLeft = _.reduceRight({ 'a': 1, 'b': 2, 'c': 3 }, function(result, value, key) {
result.push(key + value);
return result;
}, []);
// => ['c3', 'b2', 'a1'] (order may vary in older environments)Lodash reduce supports powerful iteratee shorthands for common operations through the iteratee system.
// Property name shorthand
_.reduce(objects, 'property', initialValue);
// Property path shorthand
_.reduce(objects, 'nested.property', initialValue);
// Partial object matching
_.reduce(objects, { 'property': value }, initialValue);Integration with Guarded Methods: Many lodash methods work as iteratees for reduce operations:
assign, defaults, defaultsDeepincludes, merge, orderBy, sortBy// Using guarded methods as iteratees
const merged = _.reduce([obj1, obj2, obj3], _.assign, {});undefinedundefinedarrayReduce implementation for better performanceFull compatibility with lodash's method chaining system:
_([1, 2, 3, 4])
.map(n => n * 2)
.reduce((sum, n) => sum + n, 0)
.value();
// => 20Auto-curried versions available in lodash/fp:
const fp = require('lodash/fp');
const sumNumbers = fp.reduce((sum, n) => sum + n, 0);
sumNumbers([1, 2, 3]); // => 6Respects global _.iteratee customization:
// Custom iteratee behavior affects reduce operations
_.iteratee = customIterateeFunction;
_.reduce(collection, shorthand); // Uses custom iteratee resolution