title: How to group by attribute and count a huge list of items
body: <p>I have a list of crops with their attributes such as location where they were planted, planting technique used, experience and the crop planted itself. I have managed to group them by crop and coordinates (which represents location), but they are grouped separately. Here below is my code that groups and counts them.</p>
  <pre><code>// Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  
  // Reference messages collection
  // var messagesRef = firebase.database().ref('Planting-Calendar-Entries');
  
  database = firebase.database()
  
  var ref = database.ref('Planting-Calendar-Entries');
  ref.on('value', gotData, errData);
  
  function gotData(data){
    console.log(data.val())
      var veggie = data.val();
      const counts = Object.values(veggie).reduce((counts, { Veg_planted, coordinates }) =&gt; ({
          ...counts,
          [Veg_planted]: (counts[Veg_planted] || 0) + 1,
          [coordinates]: (counts[coordinates] || 0) + 1
        }), {});
        console.log(counts);
    }
  function errData(err){
      console.log('Error!');
      console.log(err)
  }
  </code></pre>
  <p>Here is the output of the above code:</p>
  <pre><code>&quot;-35.2294059, 173.9472028&quot;: 1
  &quot;-6.2087634, 106.845599&quot;: 3 ​ 
  &quot;-1.2920659, 36.8219462&quot;: 1 ​
  &quot;-0.3688967, 35.286286&quot;: 10 ​ 
  &quot;-0.3206518, 35.21896820000001&quot;: 1 ​
  &quot;-0.1634664, 35.5931397&quot;: 1 ​ 
  &quot;-0.3452364, 35.1519724&quot;: 2 
  Ageratum: 3 
  &quot;Balloon Flower&quot;: 2
  ​Beetroot: 1
  &quot;Bells Of Ireland&quot;: 2
  Broccoli: 1
  Cabbage: 12
  </code></pre>
  <p>I would love them to be grouped and counted as below:</p>
  <pre><code>&quot;-0.3688967, 35.286286&quot;, Cabbage: 8
  &quot;-1.2920659, 36.8219462&quot;, Cabbage: 2
  &quot;-0.3206518, 35.21896820000001&quot;, Broccoli: 1
  &quot;-0.3688967, 35.286286&quot;, Beetroot: 1
  &quot;-0.3452364, 35.1519724&quot;, Ageratum: 2
  </code></pre>
  <p>If you have an idea of what I can do, I will appreciate it. Thanks!</p>