meanBy
Calculates the arithmetic mean of numbers derived from each item of an array via an iteratee.
Returns NaN for an empty array, null, or undefined — matching sumBy, which
treats null/undefined as empty rather than throwing.
Pairs with sumBy for aggregate operations.
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 average |
accessor | ByAccessor<T> | Function deriving a number from each item, or a property path |
Returns
Section titled “Returns”number — The arithmetic mean of the derived values, or NaN if the array is empty, null, or undefined
Examples
Section titled “Examples”Average a property across objects
Section titled “Average a property across objects”Averages 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.
