我正在尝试通过将Politifact API中的信息存储在名为“ supperArray”的数组中来使用信息。我这样做是通过调用API三次,然后将每个响应中的10个值推送到superArray,如下所示:
const URL1 = "https://www.politifact.com/api/statements/truth-o-meter/people/barack-obama/json/?n=50";
const URL2 = "https://www.politifact.com/api/statements/truth-o-meter/people/hillary-clinton/json/?n=210";
const URL3 = "https://www.politifact.com/api/statements/truth-o-meter/people/bernie-s/json/?n=70";
var superArray = [];
fetch("https://cors-anywhere.herokuapp.com/"+URL1)
.then(results => {
return results.json();
})
.then(data => {
data=data.filter(function(item){return item.speaker.name_slug=="barack-obama" && item.statement_type.statement_type !== "Flip";});
for(var i=0; i<10; i++){
superArray.push(data.splice(Math.random()*data.length, 1)[0]);
}
})
fetch("https://cors-anywhere.herokuapp.com/"+URL2)
.then(results => {
return results.json();
})
.then(data => {
data=data.filter(function(item){return item.speaker.name_slug=="hillary-clinton" && item.statement_type.statement_type !== "Flip";});
for(var i=0; i<10; i++){
superArray.push(data.splice(Math.random()*data.length, 1)[0]);
}
})
fetch("https://cors-anywhere.herokuapp.com/"+URL3)
.then(results => {
return results.json();
})
.then(data => {
data=data.filter(function(item){return item.speaker.name_slug=="bernie-s" && item.statement_type.statement_type !== "Flip";});
for(var i=0; i<10; i++){
superArray.push(data.splice(Math.random()*data.length, 1)[0]);
}
})
当我要求React完整记录supperArray时,如下所示:
console.log(superArray);
它记录以下内容:
这是我所期望的。但是,当我要求它记录特定值时,如下所示:
console.log(superArray[0]);
它记录的是
undefined
为什么? 最佳答案
原因是您没有在superArray
承诺中记录fetch
。如果在上一个then()
调用中将调用添加到console.log(),它将起作用。这样做的原因是,获取操作是异步执行的,这意味着在获取调用之后很久才执行获取调用之后出现的任何其他代码。
即使在提取时执行superArray
完整日志的原因也是特殊的控制台行为。
const URL1 = "https://www.politifact.com/api/statements/truth-o-meter/people/barack-obama/json/?n=50";
const URL2 = "https://www.politifact.com/api/statements/truth-o-meter/people/hillary-clinton/json/?n=210";
const URL3 = "https://www.politifact.com/api/statements/truth-o-meter/people/bernie-s/json/?n=70";
var superArray = [];
fetch("https://cors-anywhere.herokuapp.com/" + URL1)
.then(results => {
return results.json();
})
.then(data => {
data = data.filter(function(item) {
return item.speaker.name_slug == "barack-obama" && item.statement_type.statement_type !== "Flip";
});
for (var i = 0; i < 10; i++) {
superArray.push(data.splice(Math.random() * data.length, 1)[0]);
}
})
fetch("https://cors-anywhere.herokuapp.com/" + URL2)
.then(results => {
return results.json();
})
.then(data => {
data = data.filter(function(item) {
return item.speaker.name_slug == "hillary-clinton" && item.statement_type.statement_type !== "Flip";
});
for (var i = 0; i < 10; i++) {
superArray.push(data.splice(Math.random() * data.length, 1)[0]);
}
})
fetch("https://cors-anywhere.herokuapp.com/" + URL3)
.then(results => {
return results.json();
})
.then(data => {
data = data.filter(function(item) {
return item.speaker.name_slug == "bernie-s" && item.statement_type.statement_type !== "Flip";
});
for (var i = 0; i < 10; i++) {
superArray.push(data.splice(Math.random() * data.length, 1)[0]);
}
console.log(superArray[0]);
})