Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Calculate average from data in array of object and store in specific key from object

Suppose I have an array of object,

const book = [{'name':'alpha','readingTime':1231},{'name':'alpha','readingTime':1254}, 
              {'name':'beta','readingTime':190},
              {'name':'theta','readingTime':909},{'name':'theta','readingTime':10}]

I want to calculate average for each name, such that expected O/P is

**{alpha:1242.5, beta:190, theta:459.5}**

For this I tried as ,

let calculatedValue = book.reduce((acc,curr) => acc+curr.readingTime,0)/book.length

This gives me average for all the object.

I'm unable to form logic corresponding to it.

Any guidance would really be helpful. If anyone needs any further information please let me know.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You could group by name and get the count and total of readingTime and build a new object with the averages.

const
    books = [{ name: 'alpha', readingTime: 1231 }, {  name: 'alpha', readingTime: 1254 }, { name: 'beta', readingTime: 190 }, { name: 'theta', readingTime: 909 }, { name: 'theta', readingTime: 10 }],
    averages = Object.fromEntries(
        Object.entries(books.reduce((r, { name, readingTime }) => {
            r[name] = r[name] || { count: 0, total: 0 };
            r[name].count++;
            r[name].total += readingTime;
            return r;
        }, {}))
        .map(([k, { count, total }]) => [k, total / count])
    );

console.log(averages);

over 4 years ago · Santiago Trujillo Report

0

Here is another simple solution for you.

const book = [{'name':'alpha','readingTime':1231},{'name':'alpha','readingTime':1254}, 
{'name':'beta','readingTime':190},
{'name':'theta','readingTime':909},
{'name':'theta','readingTime':10}]

const result = {};

Object.values(book.reduce((acc, current) => {
    acc[current.name] = acc[current.name] || { count: 0, total: 0 };
    acc[current.name].total += current.readingTime;
    acc[current.name].count += 1;
    acc[current.name].name = current.name;

    return acc;
}, {})).forEach(({ name, count, total }) => { result[name] = total / count; });

console.log(result)

over 4 years ago · Santiago Trujillo Report

0

Need more optimization (Fell free to edit ) but it works

const book = [{'name':'alpha','readingTime':1231},{'name':'alpha','readingTime':1254}, 
              {'name':'beta','readingTime':190},
              {'name':'theta','readingTime':909},{'name':'theta','readingTime':10}]


function avgReading(arr){
let alpha = { lengtharr : 0 ,readingAll : 0 };
let beta = {...alpha};
let theta = {...alpha};
arr.forEach((item) => {
  if(item.name === 'alpha') 
    alpha.lengtharr++;
    alpha.readingAll = alpha.readingAll + item.readingTime;
  if(item.name === 'beta') 
    beta.lengtharr++;
    beta.readingAll = beta.readingAll + item.readingTime;
 if(item.name === 'theta')
    theta.lengtharr++;
    theta.readingAll = theta.readingAll + item.readingTime;
});
const obj = {
'alpha' : alpha.readingAll /alpha.lengtharr,
'beta' : beta.readingAll /beta.lengtharr,
'theta' : theta.readingAll /theta.lengtharr
}
return obj;
}
console.log(avgReading(book))

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!