我正在嘗試將大量的JSON文件加載到稍後引用的數組中,但Node始終聲明它們未定義。我沿着線代碼:使用JSON數據填充Javascript數組
var myarray = [];
(...)
var loading_num = 001; // will be incremented in a loop to load data
myarray[loading_num] = fs.readFileSync("data/" + loading_num);
(...)
var reference_num = "001"; // the number being used to pull the appropriate record
(...)
console.log(myarray[reference_num].name); // just testing to attempt to decipher why it doesn't work, I'll actually be using the data obviously
每個JSON文件具有值命名爲name
,我還沒有實現的邏輯來加載所有的人都還沒有,因爲我仍然只是試圖讓一個工作。
我誤解了JavaScript數組或對象的內容嗎?我究竟做錯了什麼?有很多文件,它們可以在數量上有所不同,所以我必須以類似的方式加載它們。
你試過'的console.log(myArray的[loading_num]。名稱);'? – wrldbt