数组中充满了对象,但事实并非如此?

The Array is full of objects but it isnt?

我已经编写了一些代码并且一切正常,新闻正在显示,但是当我尝试处理其中一条新闻时,它说它未定义

console.log(news);
console.log(news[0]);

image of the output

连新闻长度都是0

您看到的是开发工具评估惰性的情况。您在控制台中看到的输出是您单击数组时的值,而不是记录时的值。

您可以通过以下方式亲自查看:

console.log(news.length); // == 0
// Wait 2 seconds
setTimeout(() => {
  console.log(news.length); // != 0
}, 2000)

您的 news 数组在控制台日志时为空。