Options
All
  • Public
  • Public/Protected
  • All
Menu

Module reduce

Index

References

Functions Collection

References

Renames and re-exports __type

Collection Functions

  • reduce<T, R>(collection?: Record<string, T>, iteratee?: AccCollectionIteratee<T, R>, accumulator?: R): R
  • reduce<T, R>(collection?: ArrayLike<T>, iteratee?: AccCollectionIteratee<T, R>, accumulator?: R): R
  • Reduces collection to a value which is the accumulated result of running each element in collection thru 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. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection).

    Many lodash methods are guarded to work as iteratees for methods like reduce, reduceRight, and transform.

    The guarded methods are: assign, defaults, defaultsDeep, includes, merge, orderBy, and sortBy

    since

    5.0.0

    see

    reduceRight,transform

    example
    reduce([1, 2], (sum, n) => sum + n, 0)
    // => 3

    reduce({ 'a': 1, 'b': 2, 'c': 1 }, (result, value, key) => {
    (result[value] || (result[value] = [])).push(key)
    return result
    }, {})
    // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)

    Type parameters

    • T

    • R

    Parameters

    • Optional collection: Record<string, T>

      The collection to iterate over.

    • Optional iteratee: AccCollectionIteratee<T, R>

      The function invoked per iteration.

    • Optional accumulator: R

      The initial value.

    Returns R

    Returns the accumulated value.

  • Type parameters

    • T

    • R

    Parameters

    • Optional collection: ArrayLike<T>
    • Optional iteratee: AccCollectionIteratee<T, R>
    • Optional accumulator: R

    Returns R