average
Problem
You need the arithmetic mean of a numeric array, optionally extracting the value from objects.
Solution
Use average(array, callback?) to compute the mean.
ts
import { average } from '@vielzeug/arsenal';
average([1, 2, 3, 4, 5]); // 3
const products = [{ price: 10 }, { price: 20 }, { price: 30 }];
average(products, (p) => p.price); // 20Pitfalls
- Returns
NaNfor an empty array.