reduce()
reduce<
T,Accumulator>(collection,iteratee,accumulator):Accumulator
reduce<
T>(collection,iteratee):T|undefined
reduce<
T,Accumulator>(collection,iteratee,accumulator):Accumulator
Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee.
Use array.reduce() or Object.values().reduce() directly instead.
Type Parametersโ
T: Tโ
The type of elements in the array.
Accumulator: Accumulatorโ
The type of the accumulated value.
Parametersโ
Overload 1:
collection: T[]โ
The collection to iterate over.
iteratee: (accumulator, value, index, array) => Accumulatorโ
The function invoked per iteration.
accumulator: Accumulatorโ
The initial value.
Overload 2:
collection: T[]โ
The collection to iterate over.
iteratee: (accumulator, value, index, array) => Tโ
The function invoked per iteration.
Overload 3:
collection: Tโ
The collection to iterate over.
iteratee: (accumulator, value, key, object) => Accumulatorโ
The function invoked per iteration.
accumulator: Accumulatorโ
The initial value.
Returns: Accumulatorโ
The accumulated value.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
reduce (Lodash, es-toolkit, Remeda, Ramda, Effect) ยท โ (Radashi, Modern Dash, Antfu)
Exampleโ
const numbers = [1, 2, 3, 4, 5];
// โ Deprecated approach
const sum = reduce(numbers, (acc, num) => acc + num, 0);
console.log(sum); // 15
// โ
Recommended approach (for arrays)
const sumNative = numbers.reduce((acc, num) => acc + num, 0);
console.log(sumNative); // 15
// โ
Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const sumObj = Object.values(obj).reduce((acc, num) => acc + num, 0);
console.log(sumObj); // 6
How it works?โ
Reduces collection to a value by iterating elements.
Deprecated: Use array.reduce() directly.
Native Equivalentโ
// โ reduce(arr, fn, init)
// โ
arr.reduce(fn, init)
Use Casesโ
Aggregate values into single result ๐โ
Combine all elements into a single value.
const prices = [10, 20, 30, 40];
prices.reduce((sum, price) => sum + price, 0);
// => 100
Group items by keyโ
Categorize items into groups.
const items = [{ type: "a" }, { type: "b" }, { type: "a" }];
items.reduce((acc, item) => {
(acc[item.type] ??= []).push(item);
return acc;
}, {});
Build lookup mapโ
Create an object for fast lookups.
const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
users.reduce((map, u) => ({ ...map, [u.id]: u }), {});
// => { 1: { id: 1, ... }, 2: { id: 2, ... } }
Compute multiple statistics in a single passโ
Calculate several metrics (sum, count, min, max) without iterating multiple times. Efficient for analytics, reporting, and data processing pipelines.
const orders = [
{ amount: 120 },
{ amount: 45 },
{ amount: 230 },
{ amount: 89 },
];
const stats = reduce(
orders,
(acc, order) => ({
total: acc.total + order.amount,
count: acc.count + 1,
min: Math.min(acc.min, order.amount),
max: Math.max(acc.max, order.amount),
}),
{ total: 0, count: 0, min: Infinity, max: -Infinity }
);
const average = stats.total / stats.count;
// stats => { total: 484, count: 4, min: 45, max: 230 }
// average => 121