Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

javascript - Returning multiple values in an array

I have a nested array and I want to update that array with values in my database. It is based on a timetable: a 5 day week with 6 periods in each day. Monday is 0 and Friday is 4 in the array. There is period 0 up until period 5. When I execute this function it says: Cannot Set property '6' is undefined And my array is not updated When I remove the "return" line, the error does not occur but obviously my array is still not updated. Is this due to having multiple returns due to the loop. And if so how do i fix this? If it's another error please show me! If there is anymore information need please ask!

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++;
    }
question from:https://stackoverflow.com/questions/65940290/returning-multiple-values-in-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume that you have initialized the array links someplace else, because I don't see it here. The way in which you are trying to assign values (for the first time) for the multidimensional array is incorrect. Each element inside the array isn't defined as a sub-array at first. Solved this problem using a simple for loop that iterates over the first 5 variables (in this case) and assigns them as empty variables. This can also be made dynamic depending on the nature of the problem.

Oh and lastly, you combined a return statement with an assignment statement. That's incorrect.

Check this link for more information.

    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++;

This fix should get this code working right. Do let me know the outcome of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...