title: Calculate max and average of JSON API result
body: <p>I need to calculate <code>average</code> and <code>max</code> rating.</p>
  <p>From my perspective this should use <code>map</code> and <code>reduce</code> for this purpose, but, I'm not sure if the JSON has the right format, or if it needs any sanitization.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const
    myJSON = 
      [ { "page": 1, "totalPages": 2, "data": 
          [ { "title": "Developer 1", "rating": 4.7} 
          , { "title": "Developer 2", "rating": 7.8} 
      ] } ]
    ;
  var Max_JSO = myJSON.reduce( (acc, cur )=&gt; ((acc.rating &gt; cur.rating)? acc : cur) );
  var Min_JSO = myJSON.reduce( (acc, cur )=&gt; ((acc.rating &lt; cur.rating)? acc : cur) );
  
  var Avg_JSO = myJSON.reduce( (acc, cur, idx, arr )=&gt; {
    let
      sum = acc.rating + cur.rating,
      no = idx +1;
    if (no === arr.length) { sum = sum / no };
    return { 'name': cur.name, 'rating': sum }
  });
  
  console.log ('max =',  Max_JSO)  
  console.log ('min =',  Min_JSO)  </code></pre>
  </div>
  </div>
  </p>
  <pre><code>max = { page: 1, totalPages: 2, data: 
          [ { title: 'Developer 1', rating: 4.7 } 
          , { title: 'Developer 2', rating: 7.8 } 
          ] 
        } 
  
  min = { page: 1, totalPages: 2, data: 
          [ { title: 'Developer 1', rating: 4.7} 
          , { title: 'Developer 2', rating: 7.8} 
          ] 
        } 
  </code></pre>
  <p>I'm using this code right now, but I get a weird string as an output:</p>
  <pre><code>&quot;max =&quot;, &quot;{\&amp;quot;page\&amp;quot;:1,\&amp;quot;totalPages\&amp;quot;:5,\&amp;quot;data\&amp;quot;:[{\&amp;quot;title\&amp;quot;:\&amp;quot;Developer 1\&amp;quot;,\&amp;quot;rating\&amp;quot;:4.7},{\&amp;quot;title\&amp;quot;:\&amp;quot;Developer 2\&amp;quot;,\&amp;quot;rating\&amp;quot;:7.8}]}&quot;
   
  &quot;min =&quot;, &quot;{\&amp;quot;page\&amp;quot;:1,\&amp;quot;totalPages\&amp;quot;:5,\&amp;quot;data\&amp;quot;:[{\&amp;quot;title\&amp;quot;:\&amp;quot;Developer 1\&amp;quot;,\&amp;quot;rating\&amp;quot;:4.7},{\&amp;quot;title\&amp;quot;:\&amp;quot;Developer 2\&amp;quot;,\&amp;quot;rating\&amp;quot;:7.8}]}&quot;
  </code></pre>
  <p>Is there any way to do this better?</p>
  <p>Also, I need to not only read <code>rating</code> from <code>data</code> key, but also outside of it, as you can see on <code>var myJSON</code></p>
  <p>Like produce a resulting <code>json</code> like this:</p>
  <pre><code>  {
        &quot;Developer 1&quot;: 4.7,
        &quot;Developer 2&quot;: 7.8,
    }
  </code></pre>
  <p>MIN being <code>Developer 1</code> and MAX <code>Developer 2</code></p>