0

I would like to get the average of data in sub-array from json by axios call

JSON Data

  • 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) – Ohgodwhy Feb 17 '21 at 19:19

2 Answers2

0

Just parse this answer (JSON.parse), iterate over votes, sum vote values and then divide the sum by length of votes table. Simple as that.

0

Try using .reduce() like this:

averageVote(data) {
  let total = data.votes.reduce((accumulator, vote) => {
    return accumulator += vote.vote;
  }, 0);
  let count = data.votes.length;
  return total / count;
}
Mike
  • 785
  • 1
  • 6
  • 14