簡單地說,我想在JSON對象中獲取一個鍵的值,但在運行時,這個工作在設計時就像這樣。在運行時將JSON對象添加到JSON數組問題
var studentsData = [
{ "studentName": "Elen", "Birthyear": 1981 }
, { "studentName": "Stev", "Birthyear": 1987 }];
console.log(studentsData[0].studentName); // print Elen
但如果推對象數組在運行時這樣
var studentsData = [];
var jsonObj={ "studentName": "Elen", "Birthyear": 1981 }; //because i intend to get the object data at runtime
studentsData.push(jsonObj);
console.log(studentsData[0].studentName);
通過這種方式,它給我的錯誤:遺漏的類型錯誤:未定義
無法讀取屬性「studentName」請幫助,謝謝
對不起,我把另一個代碼不同的數組變量, 但似乎從一開始我真正的問題是相同的未解決此問題: - How do I assign a variable from localForage as I do in localStorage?
您正在嘗試閱讀'populationData'而不是'studentsData'? –
您需要說明您正在編寫哪種語言,並將其添加到代碼中。 – GeneCode
你錯過了什麼。第一和第二種情況下的「populationData」是什麼?由於它在#1的情況下起作用,請顯示在哪裏以及如何聲明它。 –