or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--reduce

Lodash reduce functionality for transforming arrays and objects by applying an iteratee function to accumulate results

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash@4.6.x

To install, run

npx @tessl/cli install tessl/npm-lodash--reduce@4.6.0

index.mddocs/

Lodash Reduce

Lodash 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.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash
  • Version: 4.6.0

Core Imports

// Full lodash import
const _ = require('lodash');

ES6 Import:

import _ from 'lodash';

Standalone package (if available):

const reduce = require('lodash.reduce');

Basic Usage

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'] }

Capabilities

Array and Object Reduction

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 value
  • value - The current element value
  • index|key - The current element index (arrays) or key (objects)
  • collection - The collection being iterated

Supported Collection Types:

  • Arrays and array-like objects
  • Plain objects
  • Typed arrays
  • Strings (treated as character arrays)

Right-to-Left Reduction

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)

Iteratee Shorthand Support

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, defaultsDeep
  • includes, merge, orderBy, sortBy
// Using guarded methods as iteratees
const merged = _.reduce([obj1, obj2, obj3], _.assign, {});

Error Handling

Empty Collections

  • With accumulator: Returns the accumulator value
  • Without accumulator: Returns undefined

Invalid Collections

  • null/undefined: Treated as empty collection, returns accumulator or undefined
  • Primitive values: Numbers, booleans converted to objects for iteration

Type Safety

  • All type checking handled internally
  • Consistent behavior across different JavaScript environments
  • Performance optimizations for common data types

Performance Characteristics

Optimization Strategies

  • Array Fast Path: Specialized arrayReduce implementation for better performance
  • Object Fast Path: Optimized iteration for plain objects
  • Type Detection: Runtime optimization based on collection type
  • Early Exit: Efficient handling of edge cases

Memory Efficiency

  • Single-pass iteration
  • No intermediate array creation
  • Minimal memory overhead for large collections

Integration Features

Method Chaining

Full compatibility with lodash's method chaining system:

_([1, 2, 3, 4])
  .map(n => n * 2)  
  .reduce((sum, n) => sum + n, 0)
  .value();
// => 20

Functional Programming

Auto-curried versions available in lodash/fp:

const fp = require('lodash/fp');

const sumNumbers = fp.reduce((sum, n) => sum + n, 0);
sumNumbers([1, 2, 3]); // => 6

Custom Iteratee Integration

Respects global _.iteratee customization:

// Custom iteratee behavior affects reduce operations
_.iteratee = customIterateeFunction;
_.reduce(collection, shorthand); // Uses custom iteratee resolution