Optional
collection: Record<string, T>The collection to iterate over.
Optional
iteratee: AccCollectionIteratee<T, R>The function invoked per iteration.
Optional
accumulator: RThe initial value.
Returns the accumulated value.
5.0.0
[[reduceRight]],[[transform]]
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)
Optional
collection: ArrayLike<T>Optional
iteratee: AccCollectionIteratee<T, R>Optional
accumulator: R
Reduces
collection
to a value which is the accumulated result of running each element incollection
thruiteratee
, where each successive invocation is supplied the return value of the previous. Ifaccumulator
is not given, the first element ofcollection
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
, andtransform
.The guarded methods are:
assign
,defaults
,defaultsDeep
,includes
,merge
,orderBy
, andsortBy