An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
November 22, 2021 03:58 pm GMT
Original Link: https://dev.to/martinkr/1-line-of-code-how-to-get-the-average-of-an-array-7bj
1 line of code: How to get the average of an array
const average = arr = arr.reduce((a, b) => a + b, 0) / arr.length;
Returns the average of the sum of all items in a numerical array.
Beware of JavaScripts Automatic Type Conversion if your Array
contains something else than Numbers
.
The repository & npm package
You can find the all the utility functions from this series at github.com/martinkr/onelinecode
The library is also published to npm as @onelinecode for your convenience.
The code and the npm package will be updated every time I publish a new article.
Follow me on Twitter: @martinkr and consider to buy me a coffee
Photo by zoo_monkey on Unsplash
Original Link: https://dev.to/martinkr/1-line-of-code-how-to-get-the-average-of-an-array-7bj
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To