title: Can I use reduce to turn an array into an array of objects with custom keys and also counting duplicate values?
body: <p>In my current code I have an array of names which get turned into an object with reduce(), and any duplicate names will increment a count value. </p>
  
  <p>Initial array (nameArray):</p>
  
  <pre><code>["Name1","Name2","Name2","Name2","Name3","Name4","Name5"]
  </code></pre>
  
  <p>Reduce code:</p>
  
  <pre><code>    const nameCount = nameArray.reduce((a, b) =&gt; {
          a[b] = (a[b] || 0) + 1
          return a
      }, {})
  </code></pre>
  
  <p>Reduce output:</p>
  
  <pre><code>{"Name1":1,"Name2":3,"Name3":1,"Name4":1,"Name5":1}
  </code></pre>
  
  <p>I want to change the format of this to be like so:</p>
  
  <pre><code>[
  {
  "name": Name1,
  "count": 1
  },
  {
  "name": Name2,
  "count": 3
  },
  {
  "name": Name3,
  "count": 1
  }
  ...etc
  ]
  </code></pre>
  
  <p>Is this possible by using just reduce? What would be the best, most efficient way to do this?
  I've tried changing my reduce code but haven't been able to get it to work, nor did my search for solutions result in anything. </p>
  
  <p>Any help is appreciated!</p>