I have an array modelProps which is a parsed JSON object. modelProps is assigned a value in my getModelProps() function which looks like this:
function getModelProps() {
console.log("GET MODEL PROPERTIES")
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
var myObj = JSON.parse(this.responseText);
myObj.forEach(function (record) {
modelProps.push(record)
})
}
}
xmlhttp.open("GET","./php/getModelProps.php",true);
xmlhttp.send();
}
After calling the function, when I simply log modelProps, I get the output:
This confirms that modelProps isn't empty. But the problem becomes apparent when I try to access it by other means. Firstly, assuming that modelProps was just an array, I tried doing console.log(modelProps[0])
which gave an output of undefined
. Afterwards, I logged the typeof modelProps to find out that it was an Object. After finding this out, I tried logging the entries(), keys() and values(), only to get empty arrays returned.
Can someone please explain why this is? Any help would be appreciated.