[英]Initialization and pushing data in array
首先,我聲明一個數組:
let arr = [];
因此,我通過以下方式填充此數組:
(async function loop() {
for(let i=timeStart; i<=timeEnd; i+=3600000)
{
await ExternDataProvider.getCountData(i, (i+3600000), localSearchFilters)
.then(function(dataResult){
arr.push(dataResult);
})
.catch(function(err){
});
}
})();
但是,當我在console.log中創建變量“ arr”時,會得到以下結果:
那么,為什么我沒有像[2251,215]
這樣的東西呢?
如果在調用函數后使用console.log()
數組,則控制台中將有一個空數組,但是在解析數據后,控制台將刷新該數組的內容。 但是,如果您console.log(arr[0])
會立即顯示undefined
。
在我看來,你應該回到你的陣列(或解決問題),並捕獲另一解析值then
和執行您console.log
這里面then
嘗試這個:
let arr = [];
setTimeout(function() {
arr.push(1)
}, 5000);
console.log(arr[0]); // prints undefined
console.log(arr); // prints an empty array if you expand the array before 5 seconds, and it will print [1] if you re-expand it after 5 seconds
您有[2251,215]
但是控制台以不同的方式顯示它。
0表示第 0 位的值。
1表示第 1 個位置的值
也許使用console.log(JSON.stringify(array))
做到這一點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.