我创建了一个字符串数组,格式为“monthX”,其中数字是整个数组中增加的数字。引用函数中的数组项(以字符串形式)javascript
我有一个函数,我试图引用该数组的特定项目,但它不断进来未定义。这是我的代码:
function listCategories() {
categoryList.innerHTML = ""
for (var propertyName in categoryObject) {
var rowHTML = "<div>"
rowHTML += "<span class = 'category'>" + categoryObject[propertyName].name + "</span>"
rowHTML += "<span class = '" + monthList[3] + "'><input/></span>"
rowHTML += "</div>"
categoryList.innerHTML += rowHTML
}
}
//Months to load in
for (var i=0; i<24; i++) {
monthList[i] = "month" + (i + startingMonth)
}
我感兴趣的领域是“monthList [3]”行。即使我console.log(monthList [3])它正确地说“month6”,它仍然是未定义的。有任何想法吗?我的代码中有错误吗?
这仍然有问题。你已经声明了'monthList',但你没有将它定义为一个数组。这将导致错误,如“TypeError:不能将undefined转换为对象” – JAAulde
谢谢,itsid! 当你说://不要CALL listCategories之前,你给了我答案! – isaachess
@JAAulde,uups你是对的...对不起,我错过了,那天我有太多的PHP,你可以把对象放在以前的布尔变量中,就像那样。 – itsid