javascript - Building and updating array objects -
i'm working on simple registration system displays number of seats available in particular workshop. it's built in google apps script runs javascript syntax.
i build array of class
objects user when log in. 1 of object keys seats
, shows available number of spots left in workshop. that, compare master list of classes max number of registrants current registration list.
class object
[{ date: 4/10/2017, title: "workshop 1", desc: "a string description 1", seats: "" }]
google sheets table
| date | title | description | seats | | |-----------|------------|----------------------------|-------|---| | 4/10/2017 | workshop 1 | string description 1 | 20 | | | 5/10/2017 | workshop 2 | string description 2 | 25 | |
current registrations
| user | class0 | class1 | |-------|-----------|-----------| | user1 | 4/10/2017 | | | user2 | 4/10/2017 | 5/10/2017 | | | | |
script
// set count variable var count; // sessions user not registered for(var i=0; i<sessions.length; i++) { // class in master list for(var j=0; j<allsessionsdata.length; j++) { // find stored date in master list var date = allsessionsdata[j][0]; // match session dates find max seats if(sessions[i].date === date) { sessions[i].seats = allsessionsdata[j][5]; } } } // reopen sessions loop current counts for(var i=0; i<sessions.length; i++) { var count = sessions[i].seats; // current 2d registrations array for(var j=0; j<allregsdata.length; j++) { for(var k=0; k<allregsdata[j].length; k++) { if(sessions[i].date === allregsdata[j][k]) { count--; sessions[i].seats = count; } } } } // return updated array return sessions; }
the function return 18 4/10 date , 24 5/10. script working i'm wondering if should condensed 1 loop , why. know "best practice" subjective, feels redundant build 1 array reopen within same function.
the user classes table has bad design, since cannot accomodate users take 3 classes. following preferable , lead easier code:
| user | class | |-------|-----------| | user1 | 4/10/2017 | | user2 | 4/10/2017 | | user2 | 5/10/2017 | | | |
also, without count
, use --sessions[i].seats;
sessions[i].seats
== count
Comments
Post a Comment