I just fetch some resource from a page, and I put them into an array. Below is my code.
fetch(jsonUrl, {method: "get"})
.then((response) =>{
return response.json();
}).then((data) =>{
jsonData= data.result.results;
for(let i = 0; i<jsonData.length; i++){
place=[jsonData[i]["stitle"],jsonData[i]["file"].substring(0,end+3)];
end=jsonData[i]["file"].toLowerCase().indexOf("jpg", 0);
console.log(jsonData[i]["file"].substring(0,end+3));
console.log(place);
console.log(typeof(place));
console.log(place[0]);
}
})
The question is, I want to use it outside the fetch scope like this.
let myDiv = document.getElementById("description");
let newDiv = document.createElement("div");
let textNode = document.createTextNode(place[0]);
newDiv.appendChild(textNode);
myDiv.appendChild(newDiv);
But the value of place
would be "undefined". What should I do?