Skip to content

meanBy() function

Returns the average of the values produced by applying f to each element of the given Iterable/AsyncIterable. Returns NaN for an empty iterable.

Signature:

typescript
declare function meanBy<A>(f: (a: A) => number, iterable: Iterable<A>): number;

Example

ts
meanBy((a) => a.score, [
  { name: "a", score: 80 },
  { name: "b", score: 100 },
]); // 90

meanBy((a) => a.score, []); // NaN

// with pipe
pipe(
  [{ score: 80 }, { score: 100 }],
  meanBy((a) => a.score),
); // 90

Open Source Code

Released under the Apache-2.0 License.