Skip to content

Function: reduce()

ts
function reduce<T, R>(
   collection, 
   iteratee, 
   initialValue): R;

Reduces an array or object into a single accumulated value. 将数组或对象归约为单个累计值。

Type Parameters

Type ParameterDescription
T extends Record<string, unknown> | readonly unknown[]The source collection type / 源集合类型
RThe accumulator type / 累加器类型

Parameters

ParameterTypeDescription
collectionTThe source collection / 源集合
iteratee(accumulator, value, key, collection) => RThe reducer function / 归约函数
initialValueRThe initial accumulator / 初始累加值

Returns

R

The reduced result / 归约结果

Example

ts
reduce({ a: 1, b: 2 }, (acc, value) => acc + value, 0)
// => 3

Since

1.2.0

Released under the MIT License.