So what I'm doing is getting values from a json file and pushing them to an array so that I can randomly select one and it is displayed in an html file. The problem i'm having is that even though something is in the array, it treats it as if it is empty...
var quotes = [];
var authors = [];
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'quotes.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
loadJSON(function(response) {
// Parse JSON string into object
var data = JSON.parse(response);
for(var i in data){
var key = i;
var val = data[i];
quotes.push(key);
authors.push(val);
}
});
}
init();
var index = Math.floor(Math.random()* (quotes.length + 1));
document.getElementById("p1").innerHTML = "<span class=\"quotes\">“</span>" + quotes[index] + "<span class=\"quotes\">”</span>" + "<span id=\"author\">" + " – " + authors[index] + "</span";
So in my init function it pushes the key and the val to an array but it treats it like it's empty... i'm sorry if this is a simple question but i'm pretty new at JSON. Thanks