title: Building and updating array objects
body: <p>I'm working on a simple registration system which displays the number of seats available in a particular workshop. It's built in Google Apps Script which runs with a JavaScript syntax.</p>
  
  <p>I build an array of <code>Class</code> objects for the user when they log in. One of the object keys is <code>seats</code>, which shows the available number of spots left in the workshop. To get that, I compare the master list of classes to get the max number of registrants to the current registration list.</p>
  
  <p><strong>Class Object</strong></p>
  
  <pre><code>[{ 
    date: 4/10/2017,
    title: "Workshop 1",
    desc: "A string description for 1",
    seats: ""
  }]
  </code></pre>
  
  <p><strong>Google Sheets table</strong></p>
  
  <pre><code>| date      | title      | description                | seats |   |
  |-----------|------------|----------------------------|-------|---|
  | 4/10/2017 | Workshop 1 | A string description for 1 | 20    |   |
  | 5/10/2017 | Workshop 2 | A string description for 2 | 25    |   |
  </code></pre>
  
  <p><strong>Current Registrations</strong></p>
  
  <pre><code>| user  | class0    | class1    |
  |-------|-----------|-----------|
  | user1 | 4/10/2017 |           |
  | user2 | 4/10/2017 | 5/10/2017 |
  |       |           |           |
  </code></pre>
  
  <p><strong>Script</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>// Set the count variable
    var count;
  
    // Sessions the user is not registered for
    for(var i=0; i&lt;sessions.length; i++) {
  
      // Look the class up in the master list
      for(var j=0; j&lt;allSessionsData.length; j++) {
  
        // Find the stored date in the master list
        var date = allSessionsData[j][0];
  
        // match the session dates to find the max seats
        if(sessions[i].date === date) {
          sessions[i].seats = allSessionsData[j][5];
        } 
      }
    }
  
    // Reopen the sessions loop to get the current counts
    for(var i=0; i&lt;sessions.length; i++) {
      var count = sessions[i].seats;
  
      // Get the current 2D registrations array
      for(var j=0; j&lt;allRegsData.length; j++) {
        for(var k=0; k&lt;allRegsData[j].length; k++) {
  
          if(sessions[i].date === allRegsData[j][k]) {
            count--;
            sessions[i].seats = count;
          }
        }
      }
    }
    // Return the updated array
    return sessions;
  }
  </code></pre>
  
  <p>The function would return 18 for the 4/10 date and 24 for the 5/10. The script is working but I'm wondering if this should be condensed into one loop and why. I know "best practice" is subjective, but it feels redundant to build one array only to reopen it immediately within the same function.</p>