Computes the arithmetic mean of the numeric values produced for each element in the sequence.
Type of elements within the source iterable.
source
The source iterable.
Optional
Optional projection that extracts the numeric value for each element. Defaults to the element itself.
The arithmetic mean of the selected values.
Thrown when source is empty.
Provide a selector when the elements are not already numeric. All values are enumerated exactly once.
const numbers = [1, 2, 3, 4, 5];const avg = average(numbers);console.log(avg); // 3const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 },];const avgAge = average(people, p => p.age);console.log(avgAge); // 30 Copy
const numbers = [1, 2, 3, 4, 5];const avg = average(numbers);console.log(avg); // 3const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 },];const avgAge = average(people, p => p.age);console.log(avgAge); // 30
Computes the arithmetic mean of the numeric values produced for each element in the sequence.