title: Reduce array by counting repeated and remap keys
body: <p>I have an array</p>
  <pre><code>[{fruit: &quot;Apple&quot;, cost: 2},{fruit: &quot;Banana&quot;, cost: 3}, {fruit: &quot;Apple&quot;, cost: 4}]
  </code></pre>
  <p>I just want an array of how many instances of each fruit in this array. So I used: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#remove_duplicate_items_in_an_array" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#remove_duplicate_items_in_an_array</a>.<br />
  except that it gives me</p>
  <pre><code>[{&quot;Apple&quot;, 2}, {&quot;Banana&quot;, 1}] 
  </code></pre>
  <p>and I'd like it to be</p>
  <pre><code>[{fruit:&quot;Apple&quot;, count:2}, {fruit:&quot;Banana&quot;, count:1}] 
  </code></pre>
  <p>How to remap it to have the desired keys?</p>
  <pre><code>const reduced = array.reduce((allTypes, item) =&gt; {
      if (item in allTypes) {
          allTypes[item] ++;
      }
      else {
      allTypes[item] = 1;
      }
  
      return allTypes;
  }, []);
  </code></pre>