https://github.com/iamnapo/average
🧮 Small utility function to calculate the average value of an array.
https://github.com/iamnapo/average
array average math mean
Last synced: about 6 hours ago
JSON representation
🧮 Small utility function to calculate the average value of an array.
- Host: GitHub
- URL: https://github.com/iamnapo/average
- Owner: iamnapo
- License: mit
- Created: 2021-06-29T18:27:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-26T23:26:56.000Z (11 months ago)
- Last Synced: 2025-11-23T07:29:44.794Z (3 months ago)
- Topics: array, average, math, mean
- Language: TypeScript
- Homepage: https://npm.im/@iamnapo/average
- Size: 30.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @iamnapo/average
> Small utility function to calculate the average value of an array
[](https://github.com/iamnapo/average/actions) [](https://www.npmjs.com/package/@iamnapo/average)
## Install
```sh
$ npm i @iamnapo/average
```
## Usage
```js
import average from "@iamnapo/average";
average([0, 1, 2, 3, 4, 5]); // => 2.5
average([0, 1, 2, 3, 4, 5], { excludeZeroes: true }); // => 3
average([0, 1, 2, Number.NaN, undefined], { excludeFalsies: true }); // => 1.5
average([{ name: "iamnapo", age: 28 }, { age: 28 }, { name: "Alice", age: 25 }], { get: (v) => v.age }); // => 27
average([]); // => NaN
```
## API
### average(input, options?)
Get the average value from an array.
#### input
Type: `unknown[]`
Input array.
#### options
Type: `object`
You can specify the below options.
##### excludeZeroes
Type: `boolean`\
Default: `false`
Exclude zero values from the calculation.
##### excludeFalsies
Type: `boolean`\
Default: `false`
Exclude [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) values from the calculation.
##### get
Type: `Function`\
Default: `(v) => v`
Function used to extract each value.