I'm trying to use information from the Politifact API by storing it in an array called "supperArray." I do so by calling the API three times, and pushing 10 values from each response to superArray, like so:
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]);
}
})
When I ask React to log supperArray in its entirety, like so:
console.log(superArray);
It logs this:
Which is what I expected. But when I ask it to log a specific value, like so:
console.log(superArray[0]);
It logs is undefined Why is this?
superArrayis at a later point of time than when you're calling console.log. console cannot fool you withsuperArray[0]though.