You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
561 B

4 years ago
  1. # `avg()`
  2. The avg method returns the average of all items in the collection:
  3. ```js
  4. collect([1, 3, 3, 7]).avg();
  5. // 3.5
  6. ```
  7. If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:
  8. ```js
  9. const collection = collect([
  10. {
  11. name: 'My story',
  12. pages: 176,
  13. },
  14. {
  15. name: 'Fantastic Beasts and Where to Find Them',
  16. pages: 1096,
  17. },
  18. ]);
  19. collection.avg('pages');
  20. // 636
  21. ```
  22. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/avg.js)