Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
The
avg() method returns the average of all the items in a collection.
Installation:
Syntax:
collect(array).avg()
Parameters: The collect() takes one argument that is converted into the collection and then avg() function is applied on it, which can take element if you apply it on the collection of objects.
Return Value: Returns a number which is average of the collection.
Below example illustrate the
avg() method in JavaScript:
Example 1: Here collect = require('collect.js') is used to import the collect.js library into the file.
Output
👁 Image
Example 2: Array of objects.