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