I make an Ajax request to an API and get back an array but want to use it outside function, all as a global variable.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
var latitudes = [];
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var records = xmlDoc.firstChild.children[2].children;
var resultLat
var arrayLat = [];
latitudes = arrayLat;
var i
for (i = 0; i < records.length; i++) {
resultLat.push(records[i].children[6].innerHTML);
}
arrayLat = resultLat.filter(function(record) {
return record != "Kulturlämning";
});
//Works
console.log(arrayLat);
latitudes = [arrayLat];
}
//Empty
console.log(latitudes);