meanBy

math

This method is like mean except that it accepts iteratee which is invoked for each element in array to generate the value to be averaged.

Signature

meanBy<T>(array: T[], iteratee: (value: T) => number): number

Parameters

NameTypeDescription
arrayT[]The array to iterate over
iteratee(value: T) => numberThe iteratee invoked per element

Returns

number - Returns the mean

Examples

With objects

import { meanBy } from 'dashlite'

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }]
const result = meanBy(objects, (o) => o.n)
console.log(result)

Output:

5

Available since version 1.0.0