Google News
logo
Collect.js - Interview Questions
What is avg() method in Collect.js?
The avg() method returns the average of all the items in a collection.
 
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.
 
avg() method example :
const collect = require('collect.js');
  
let arr = [
    {
      name: 'Ramana',
      score: 98,
    },
    {
      name: 'Venkat',
      score: 96,
    },
    {
        name: 'Suresh',
        score: 80
    },
  ];
  
// converting object to collection
const collection = collect(arr);
    
// finding the average of all the score
let averageScore = collection.avg('score');
    
console.log("Average score of students : ", averageScore);
Output :
Average score of students : 91.33333333333
Advertisement