1
我試圖將密鑰從for循環傳遞到另一個函數,並從那裏獲取對象的數據。出於某種原因,當我通過密鑰時,它告訴我它是未定義的。javascript:將密鑰從for循環傳遞到另一個函數失敗
//this is the for loop from where I call anotherFunction
for (var key in object) { // note that this is not an array but an object
if (object.hasOwnProperty(key)) {
console.log("test :"+ anotherObject[key]["anotherProperty"]); //this works
anotherFunction(key);
}
}
function anotherFunction(arg){
console.log("arg: "+arg); //shows the correct argument
console.log("test2: "+anotherObject["myProperty"]["anotherProperty"]); //this works, in other words, if I manually type the correct property it works but I want to be able to use the passed along argument
console.log("test3: "+anotherObject[arg]["anotherProperty"]); //this doesn't work
var arg2 = '"'+arg+'"'; //I tried adding quote characters
console.log("test4: "+anotherObject[arg2]["anotherProperty"]); //this doesn't work
}
它顯示的錯誤是: 遺漏的類型錯誤:在等
任何想法,爲什麼它不會工作不能讀取屬性未定義「anotherProperty」,我怎麼能得到它的工作?
感謝你是對的,一些奇怪的事情與我的對象! – GR4