-1

Suppose I have an array of object as,

const BookDetails = [
  { bookName: 'Harry Pottar', readingTime: 10663 },
  { bookName: 'Harry Pottar', readingTime: 10986 },
  { bookName: 'kaptura Tech', readingTime: 7034 }
]

For this I tried solution as,

const avgInvoiceProcessingTime = Math.round(((readingTime.map(a => a.Time).reduce((a, b) => a + b, 0) / 1000)));

But it doesnot give average.

I want to count all the readingTime and divide by length of BookDetails array, but having trouble adding all the reading time.

Siva Pradhan
  • 791
  • 1
  • 6
  • 23
  • 1
    _“But it doesnot give average.”_ - it should give you an _error_ first of all, because there is no variable `readingTime`. – CBroe Apr 12 '21 at 10:56
  • 1
    You want `BookDetails.map(a => a.readingTime).reduce((a, b) => a + b, 0)` to sum up all those `readingTime` values, and get `28683`. Why you’d want to divide it by 1000 then, if you want the average - unclear. – CBroe Apr 12 '21 at 10:59
  • Because it is in ms and want to convert it to second. – Siva Pradhan Apr 12 '21 at 11:01
  • Okay, but then you still need to divide by the _number_ of items you summed up there. – CBroe Apr 12 '21 at 11:09
  • 1
    Does this answer your question? [How to compute the sum and average of elements in an array?](https://stackoverflow.com/questions/10359907/how-to-compute-the-sum-and-average-of-elements-in-an-array) – Eyeslandic Apr 12 '21 at 11:15

3 Answers3

2

You can easily achieve this using reduce. Add all readingTime and divide it by the length of the readingTime.

readingTime is property of an object and it is not an array.

You can only use reduce on Array.

You need to divide the total(addition of readingTime) with BookDetails.length

const BookDetails = [{
    bookName: "Harry Pottar",
    readingTime: 10663
  },
  {
    bookName: "Harry Pottar",
    readingTime: 10986
  },
  {
    bookName: "kaptura Tech",
    readingTime: 7034
  },
];

const result =
  BookDetails.reduce((acc, curr) => acc + curr.readingTime, 0) /
  BookDetails.length;
console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
2

you were right in using reduce, but you needed to use it on the array:

const BookDetails = [
  { bookName: 'Harry Pottar', readingTime: 10663 },
  { bookName: 'Harry Pottar', readingTime: 10986 },
  { bookName: 'kaptura Tech', readingTime: 7034 }
]

const sum = BookDetails.reduce((acc,cur) => acc + cur.readingTime,0);
const count = BookDetails.length;
const avg = sum / count;

console.log(avg)
malarres
  • 2,941
  • 1
  • 21
  • 35
1

You can use reduce to calculate the sum of all the readingTime and also convert them into seconds. And then finally divide the result by the no. of items in the array.

const BookDetails = [
  { bookName: "Harry Pottar", readingTime: 10663 },
  { bookName: "Harry Pottar", readingTime: 10986 },
  { bookName: "kaptura Tech", readingTime: 7034 },
];

BookDetails.reduce((r, o) => r + o.readingTime / 1000, 0) / BookDetails.length;
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28