【问题标题】:trouble accessing elements of an array with for loop使用 for 循环访问数组元素时遇到问题
【发布时间】:2022-01-07 01:06:00
【问题描述】:

我是 java 脚本的新手,希望能得到一些帮助。我环顾谷歌,但我找不到任何与我的问题相匹配的东西,尽管我确信它在某处被覆盖。如果有的话,我提前道歉。我的问题是使用 for 循环访问数组的元素。如果我使用特定数字,我可以访问元素,例如test[1].property 但不是for(var i=0; i<1;i++){test[i].property}。后者在访问未定义的属性时返回错误。

function generateTestCases() {
    const body = document.getElementsByTagName("body")[0];

    const tbl = document.createElement("table");
    const tblBody = document.createElement("tbody");
    //array of test cases to print to the page
    let testCases = [
    createNewTestCase(5,47),
    createNewTestCase(3, 0o0),
    createNewTestCase(7,29),
    createNewTestCase(5,30),
    createNewTestCase(5,45),
    createNewTestCase(4,15),
    createNewTestCase(6,35),
    createNewTestCase(3,30),
    createNewTestCase(10,57),
    createNewTestCase(12,45)
    ]

    for (var i = 0; i < 12; i++) {
        let row = document.createElement("tr");
        //hours column of table
            let cell = document.createElement("td");
            let hours = testCases[i].hours;
            let cellText = document.createTextNode(`${hours}:`);
            cell.appendChild(cellText);
            row.appendChild(cell);
        //minutes column of table
            cell = document.createElement("td");
            let minutes = testCases[i].minutes;
            cellText = document.createTextNode(`${minutes}`);
            cell.appendChild(cellText);
            row.appendChild(cell);
        //output column of table
            cell = document.createElement("td");
            let output = testCases[i].output;
            cellText = document.createTextNode(`${output}`);
             cell.appendChild(cellText);
             row.appendChild(cell);
        //add finished row to table
        tblBody.appendChild(row);
    }

    tbl.appendChild(tblBody);
    body.appendChild(tbl);
    tbl.setAttribute("border", "1");
}

function createNewTestCase(hoursTest, minutesTest){
    const testCase = {
        hours: hoursTest,
        minutes: minutesTest,
        output: convertTimeToWords(hoursTest, minutesTest)
    };
    return testCase;
}

【问题讨论】:

    标签: javascript arrays javascript-objects


    【解决方案1】:

    您的 for 循环中的范围似乎太高了。你让它从 0-12 运行,即 13 个条目。但是,在您的 testCases 数组中只有 10 个条目:

        let testCases = [
        createNewTestCase(5,47),
        createNewTestCase(3, 0o0),
        createNewTestCase(7,29),
        createNewTestCase(5,30),
        createNewTestCase(5,45),
        createNewTestCase(4,15),
        createNewTestCase(6,35),
        createNewTestCase(3,30),
        createNewTestCase(10,57),
        createNewTestCase(12,45)
        ]
    

    尝试在循环开始时将范围从 12 减少到 9

    for (var i = 0; i &lt; 9; i++) {

    【讨论】:

    • 哇,我感到很愚蠢,谢谢你抽出宝贵的时间,不敢相信我错过了。
    • @jmaslak 当然可以,如果这对您有帮助,请接受这个作为答案或投票。
    • 更好:for (var i = 0; i &lt; testCases.length; i++),尽可能避免硬编码值。
    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多