sumBy
mathThis method is like sum except that it accepts iteratee which is invoked for each element in array to generate the value to be summed.
Signature
sumBy<T>(array: T[], iteratee: (value: T) => number): numberParameters
| Name | Type | Description |
|---|---|---|
array | T[] | The array to iterate over |
iteratee | (value: T) => number | The iteratee invoked per element |
Returns
number - Returns the sum
Examples
With objects
import { sumBy } from 'dashlite'
const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]
const result = sumBy(objects, (o) => o.n)
console.log(result)Output:
20Available since version 1.0.0