首页 > 解决方案 > JavaScript 循环是否正确?

问题描述

我想遍历一个学生的每个时期,并找到在特定日期的特定时期有空的学生的名字。以下是我尝试过的方法,但它不起作用。下面给出了 MongoDB 的 JSON

正在工作的原始代码是:

const monday = await Timetable.find({ "monday.first": true })
let names = monday.map(a => a.name)
console.log(names)

{
    "name" : "JOHN DOE",
    "regNumber": "12345",
    "monday": [{"first":true},{"second":true}],
    "tuesday": [{"third":true},{"seventh":true}]
}

    slots = . `[first,second,third,fourth,fifth,sixth,seventh,eight,ninth,tenth]
   days = [monday,tuesday,wednesday,thursday,friday]
   for(day in days){
    for(slot in slots){
    const day  = await Timetable.find({"monday.slot":true})
    let names = day.map(a=>a.name)
    console.log("Students free in "+slot+" are: "+names)
    }   
}

但是我遇到了一个错误

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读