sumBy
Calculates the sum of numbers derived from each item of an array via an iteratee.
null and undefined are treated as empty arrays and return 0.
Available since v3.0.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | null | undefined | The array of items to sum |
accessor | ByAccessor<T> | Function deriving a number from each item, or a property path |
Returns
Section titled “Returns”number — The sum of all derived values, or 0 for an empty array, null, or undefined
Examples
Section titled “Examples”Sum a property across objects
Section titled “Sum a property across objects”Sums a derived numeric value instead of the items themselves.
Use a property path instead of a function
Section titled “Use a property path instead of a function”A string (or key array) path is shorthand for a getter function.
