I am currently working on a loop script for my application that checks for the full capacity of a user array and adds a user ID if there is space available.
The data in my JSON file is structured around MongoDB
and contains 24 entries (hours). Each entry includes a mins
array (as shown below).
I have attempted to use the script below, but it needs to evaluate each array individually based on the min
value.
const hour = await db.Timer.findOne({hour: time[1]}) // finding the hour in the database
const timer = await db.Timer.findOne({hour: time[1]}, { mins: {"$elemMatch": {min: time[2]}}}) // locating a specific minute
let maxUsers = 3
for(let i = 0; i < hour.mins[i].users.length; i++) {
if(hour.mins[i].users.length < maxUsers){
console.log(i)
}
}
hour
and timer
are arrays that can be accessed using time[1]
(hour) and time[2]
(minute). This is the information I need to work with.
// sample 'mins' array structure
[
{
users: [ 432143, 543254, 6545423 ],
min: 21
},
{
users: [ 3421, 432143, 43213 ],
min: 22
},
{ users: [ 7735 ], min: 23 },
{ users: [], min: 20 }
]
// 'hour' output
{
hour: 15,
mins: [
{ users: [Array], min: 21 },
{ users: [Array], min: 22 },
{ users: [Array], min: 23 },
{ users: [], min: 20 }
]
}
// 'timer' output
{
mins: [ { users: [Array], min: 21 } ]
}
I would appreciate any suggestions on how to assess the arrays based on the minute value.