title: How to use reduce() in ES6 for calculating average?
body: <p>I am trying to extract <code>econ</code> and <code>season</code> from JSON data (see screenshot below). I want to find average economy rate per season </p>
  
  <p>eg: <code>season = 2018 economy rate = 10,20,30 so avg economy rate = (10+20+30)/3 = 20</code></p>
  
  <p><code>season = 2017 economy rate = 30,40,50 so avg economy rate = (30+40+50)/3 = 40</code></p>
  
  <p>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.</p>
  
  <p>Below is the code where I am trying to make use of <code>reduce()</code> in ES6:</p>
  
  <pre><code>const economy = bowldata.reduce( (a,{season, econ})  =&gt; {
                      a[season] = a[season] + parseInt(econ);
                      return a; 
                  }, {});
  console.log(economy);
  </code></pre>
  
  <p>I am getting <code>2018:NaN</code> 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.</p>
  
  <p>Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/bgf9x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bgf9x.png" alt="enter image description here"></a></p>
  
  <p>Let's say I have JSON data:</p>
  
  <pre><code>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
      },
  ]
  </code></pre>
  
  <p>Now the object which <code>reduce()</code> should return is key value pairs of year and average economy rate.</p>
  
  <p>Result: <code>[ 2018:20 , 2017:45 , 2016:10 ]</code></p>