javascript说array.length是未定义的,但不是数组

javascript说array.length是未定义的,但不是数组,javascript,arrays,reactjs,api,react-native,Javascript,Arrays,Reactjs,Api,React Native,我试图通过将Politifact API中的信息存储在一个名为“supperArray”的数组中来使用它。为此,我调用了三次API,并将每个响应中的10个值推送到superArray,如下所示: constURL1=”https://www.politifact.com/api/statements/truth-o-meter/people/barack-obama/json/?n=50"; 常量URL2=”https://www.politifact.com/api/statements/t

我试图通过将Politifact API中的信息存储在一个名为“supperArray”的数组中来使用它。为此,我调用了三次API,并将每个响应中的10个值推送到superArray,如下所示:

constURL1=”https://www.politifact.com/api/statements/truth-o-meter/people/barack-obama/json/?n=50";
常量URL2=”https://www.politifact.com/api/statements/truth-o-meter/people/hillary-clinton/json/?n=210";
常量URL3=”https://www.politifact.com/api/statements/truth-o-meter/people/bernie-s/json/?n=70";
var superArray=[];
取回(“https://cors-anywhere.herokuapp.com/“+URL1)
。然后(结果=>{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){return item.speaker.name\u slug==“barack obama”&&item.statement\u type.statement\u type!==“Flip”});
对于(var i=0;i{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){return item.speaker.name\u slug==“hillary clinton”&&item.statement\u type.statement\u type!==“Flip”});
对于(var i=0;i{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){return item.speaker.name\u slug==“bernie-s”&&item.statement\u type.statement\u type!==“Flip”});

对于(var i=0;i原因是您没有在
fetch
promise中记录
superArray
。如果您在上一次
中添加对console.log()的调用,则()
call它可以工作。原因是fetch是异步执行的,这意味着在fetch调用之后出现的任何附加代码都是在fetch返回任何内容之前执行的

即使在提取之外执行,也可以看到
superArray
的完整日志的原因是一种特殊的控制台行为

constURL1=”https://www.politifact.com/api/statements/truth-o-meter/people/barack-obama/json/?n=50";
常量URL2=”https://www.politifact.com/api/statements/truth-o-meter/people/hillary-clinton/json/?n=210";
常量URL3=”https://www.politifact.com/api/statements/truth-o-meter/people/bernie-s/json/?n=70";
var superArray=[];
取回(“https://cors-anywhere.herokuapp.com/“+URL1)
。然后(结果=>{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){
return item.speaker.name\u slug==“巴拉克·奥巴马”&&item.statement\u type.statement\u type!==“翻转”;
});
对于(变量i=0;i<10;i++){
superArray.push(data.splice(Math.random()*data.length,1)[0]);
}
})
取回(“https://cors-anywhere.herokuapp.com/“+URL2)
。然后(结果=>{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){
return item.speaker.name\u slug==“hillary clinton”&item.statement\u type.statement\u type!==“Flip”;
});
对于(变量i=0;i<10;i++){
superArray.push(data.splice(Math.random()*data.length,1)[0]);
}
})
取回(“https://cors-anywhere.herokuapp.com/“+URL3)
。然后(结果=>{
返回results.json();
})
。然后(数据=>{
data=data.filter(函数(项){
return item.speaker.name\u slug==“bernie-s”和&item.statement\u type.statement\u type!==“Flip”;
});
对于(变量i=0;i<10;i++){
superArray.push(data.splice(Math.random()*data.length,1)[0]);
}
console.log(superArray[0]);

})
在获取数据之前可以登录到您的控制台。为确保这一点,请在完成数据后,即在
内记录。然后()

或者,您可以使用:

superArray.length && console.log(superArray[0]);

我认为在您的console.log之前,还没有执行URL的获取


在输出到控制台之前,请确保所有承诺都已执行。

您在哪里打印阵列?我假设控制台在比您调用console.log更晚的时间点显示
superArray
是在愚弄您。不过,控制台无法用
superArray[0]
愚弄您。
superArray.length && console.log(superArray[0]);