繁体   English   中英

返回数组中的多个值

[英]Returning multiple values in an array

我有一个嵌套数组,我想用我的数据库中的值更新该数组。 它基于一个时间表:每周 5 天,每天 6 个课时。 数组中星期一为 0,星期五为 4。 期间 0 到期间 5。当我执行此 function 时它说:无法设置属性 '6' 未定义并且我的数组未更新当我删除“返回”行时,错误不会发生但显然我的数组是仍然没有更新。 这是由于循环导致多次返回吗? 如果是这样,我该如何解决? 如果是另一个错误,请告诉我! 如果还有信息需要请询问!

dayCountTest = 0;
    while (dayCountTest<5){
        console.log("2");
        if (dayCountTest == "0") {
            tempDay = "Monday";
        } else if (dayCountTest ===  "1") {
            tempDay = "Tuesday";
        } else if (dayCountTest == "2") {
            tempDay = "Wednesday";
        } else if (dayCountTest == "3") {
            tempDay = "Thursday";
        } else if (dayCountTest == "4") {
            tempDay = "Friday";
        }   

        periodCount = 0;
        while (periodCount < 6) {
            console.log("3");
            let db = new sqlite.Database('./linksdb.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
            let sql = `SELECT Link FROM `+ tempDay+`
                        WHERE Period = `+ periodCount + ``;

            db.all(sql, [], (err, rows) => {
                if (err) {
                    throw err;
                }
                
                rows.forEach((row) => {
                    return links[dayCountTest][periodCount] = row.Link;
                    
                });
            });
            db.close();
            periodCount++;
        }
        dayCountTest++;
    }

我假设您已经在其他地方初始化了数组链接,因为我在这里看不到它。 您尝试为多维数组(第一次)赋值的方式不正确。 数组中的每个元素最初都没有定义为子数组。 使用一个简单的 for 循环解决了这个问题,该循环遍历前 5 个变量(在本例中)并将它们分配为空变量。 这也可以根据问题的性质动态化。

哦,最后,您将 return 语句与赋值语句组合在一起。 那是不正确的。

检查链接以获取更多信息。

    dayCountTest = 0;

    links = []; // declaring the array
    for (var i = 0; i < 5; i++) {
        links[i] = [,,,,,]; // making the first 5 elements of the link array as empty arrays of 6 length as there are six periods in this case
    }

    while (dayCountTest<5){
        console.log("2");
        if (dayCountTest == "0") {
            tempDay = "Monday";
        } else if (dayCountTest ===  "1") {
            tempDay = "Tuesday";
        } else if (dayCountTest == "2") {
            tempDay = "Wednesday";
        } else if (dayCountTest == "3") {
            tempDay = "Thursday";
        } else if (dayCountTest == "4") {
            tempDay = "Friday";
        }   

        periodCount = 0;
        while (periodCount < 6) {
            console.log("3");
            let db = new sqlite.Database('./linksdb.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
            let sql = `SELECT Link FROM `+ tempDay+`
                        WHERE Period = `+ periodCount + ``;

            db.all(sql, [], (err, rows) => {
                if (err) {
                    throw err;
                }
                
                rows.forEach((row) => {
                    links[dayCountTest][periodCount] = row.Link;
                    
                });
            });
            db.close();
            periodCount++;
        }
        dayCountTest++;

此修复程序应该使此代码正常工作。 请让我知道这件事的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM