I am trying to extract econ
and season
from JSON data (see screenshot below). I want to find average economy rate per season
eg: season = 2018 economy rate = 10,20,30 so avg economy rate = (10+20+30)/3 = 20
season = 2017 economy rate = 30,40,50 so avg economy rate = (30+40+50)/3 = 40
So I want to calculate sum of all the economy rate for particular season and then take the average i.e divide the total sum for particular season by number of matches in that season.
Below is the code where I am trying to make use of reduce()
in ES6:
const economy = bowldata.reduce( (a,{season, econ}) => {
a[season] = a[season] + parseInt(econ);
return a;
}, {});
console.log(economy);
I am getting 2018:NaN
but instead I want my output to be 2018:22 , 2017: 50, etc. i.e I want key value pairs of season and average economy rate for that season.
Screenshot:
Let's say I have JSON data:
var bowldata = [
{
season:2018
economy:10
},
{ season:2018
economy:20
},
{ season:2018
economy:30
},
{ season:2017
economy:40
},
{ season:2017
economy:50
},
{ season:2016
economy:10
},
]
Now the object which reduce()
should return is key value pairs of year and average economy rate.
Result: [ 2018:20 , 2017:45 , 2016:10 ]