title: How to use javascript reduce function to calculate average of items meeting a specific condition?
body: <p>So assume I have the following array of objects:</p>
  
  <pre><code>var arr = [
    {"name": "John", "score": "8.8"},
    {"name": "John", "score": "8.6"},
    {"name": "John", "score": "9.0"},
    {"name": "John", "score": "8.3"},
    {"name": "Tom",  "score": "7.9"}
  ];
  
  var count = 0;
  var avgScore = arr.reduce(function (sum,person) {
    if (person.name == "John") {
      count+=1;
      return sum + parseFloat(person.score);
    }
    return sum;
  },0)/count);
  </code></pre>
  
  <p>Question: Is there a way way to calculate the average score for "John" without creating a global <strong>count</strong> variable.  Ideally, the count would be internal to the anonymous function in the arr.reduce.</p>