sumBy es-toolkit
Calculates the sum of array elements using a transformation function.
Usage
sumBy(items, getValue)
Use sumBy when you want to convert each element of an array to a number and calculate the sum. It's useful for summing specific properties from an array of objects.
Complex calculations are also possible.
Real-world use case examples.
Empty arrays return 0.
Parameters
itemsreadonly T[]required
The array to calculate the sum from.
getValue(element: T) => numberrequired
A function that converts each element to a number.
Returns
Returns the sum of values after applying the transformation function. Returns 0 for empty arrays.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.