title: Create an array of objects from Firebase of specified keys and values with forEach loop
body: <p>I need to return a single array of objects from Firebase that I can then manipulate. </p>
  
  <p>The code below logs many arrays of incrementing length. I only need the last array that has the total number of objects with the data available in the child I'm referencing. Once I have this array I need to manipulate it. I'm dealing with court data. <code>weeklyData</code> is just the number of people held for x nights before their court appearance. </p>
  
  <pre class="lang-js prettyprint-override"><code>var weeklyNightsHeld = db.ref(`nightsHeld/weeklyNightsHeld`);
  var array = [];
  
  weeklyNightsHeld.on("child_added", function(snapshot) {
  
    snapshot.forEach(function(data) {
      var weeklyData = new Object();
      weeklyData.days = data.key;
      weeklyData.people = data.val();
  
      array.push(weeklyData)
  
      console.log(array);
  });
    });
  </code></pre>
  
  
  
  <p>Here is my Firebase data:</p>
  
  <pre class="lang-js prettyprint-override"><code> {
    "nightsHeld" : {
      "weeklyNightsHeld" : {
        "-KgmbMAtaK7AvkaPWv-K" : {
          "1" : 6,
          "5" : 8,
          "6" : 12
        },
        "-Kgmbt-TbnZ8CRHamHnR" : {
          "2" : 8,
          "5" : 8,
          "6" : 12
        },
        "-Kgmc8U6LIBBXYBvBfu_" : {
          "5" : 8,
          "6" : 12,
          "7" : 10
        },
        "-KgmcIxN4CJTROqn3Ihu" : {
          "5" : 8,
          "6" : 12
        }
      }
    }
  }
  </code></pre>
  
  <p>
  Currently, <code>console.log(array);</code> logs the following arrays that increment in length:</p>
  
  <pre><code> [ { days: '1', people: 6 } ]
  [ { days: '1', people: 6 }, { days: '5', people: 8 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 },
    { days: '6', people: 12 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '7', people: 10 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '7', people: 10 },
    { days: '5', people: 8 } ]
  [ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '7', people: 10 },
    { days: '5', people: 8 },
    { days: '6', people: 12 } ]
  </code></pre>
  
  <p>I only need to retrieve <strong>one array</strong>, the <strong>last array</strong> logged by <code>console.log(array);</code>: </p>
  
  <pre><code>[ { days: '1', people: 6 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '2', people: 8 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '5', people: 8 },
    { days: '6', people: 12 },
    { days: '7', people: 10 },
    { days: '5', people: 8 },
    { days: '6', people: 12 } ]
  </code></pre>
  
  <p>Then I need to sum the total number of people for each "days" key with a common value. 
  For example, I would want this new array:   </p>
  
  <pre><code> [{ days: '1', people: 6 },
    { days: '5', people: 32 },
    { days: '6', people: 48 },
    { days: '2', people: 8 },
    { days: '7', people: 10 }]
  </code></pre>
  
  <p>Im using web admin in Node.js to access my Firebase realtime database. </p>