I would like to get the average of data in sub-array from json by axios call
Asked
Active
Viewed 75 times
2 Answers
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.
Maciej Pankanin
- 48
- 7
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