使用 for 循环访问数组元素时遇到问题
trouble accessing elements of an array with for loop
我是 java 脚本的新手,希望得到一些帮助。我环顾四周 google 但我找不到任何与我的问题相匹配的东西,尽管我确信它已被覆盖在某个地方。如果有的话,我提前道歉。我的问题是使用 for 循环访问数组的元素。如果我使用特定的数字,我就可以访问这些元素,例如test[1].property
但不是 for(var i=0; i<1;i++){test[i].property}
。后者 returns 访问未定义的 属性 时出错。
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;
}
您的 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 < 9; i++) {
我是 java 脚本的新手,希望得到一些帮助。我环顾四周 google 但我找不到任何与我的问题相匹配的东西,尽管我确信它已被覆盖在某个地方。如果有的话,我提前道歉。我的问题是使用 for 循环访问数组的元素。如果我使用特定的数字,我就可以访问这些元素,例如test[1].property
但不是 for(var i=0; i<1;i++){test[i].property}
。后者 returns 访问未定义的 属性 时出错。
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;
}
您的 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 < 9; i++) {