I am fairly new to JavaScript and I have encountered a peculiar issue while trying to access items stored in an Array within a JavaScript object.
Code
var Exp = {"no":Array(), "dateFrom":Array()}
readData();
function readData() {
const xhr = new XMLHttpRequest();
xhr.open('GET', sheet, true);
xhr.onload = function () {
if (this.status === 200) {
const response = JSON.parse(this.responseText);
const feeds = response.feed.entry;
feeds.forEach(function (post) {
Exp["no"].push(post.gsx$no.$t);
Exp["dateFrom"].push(post.gsx$from.$t);
});
}
}
xhr.send();
};
var lengthNo = Object.keys(Exp.no).length;
var valueZero = Exp.no[0];
console.log(lengthNo, valueZero, Exp);
Console result
0 undefined
{no: Array(0), dateFrom: Array(0)}
no: (4) ["1", "2", "3", "4"]
dateFrom: (4) ["02/2001", "07/2006", "11/2008", "03/2014"]
__proto__: Object
When running this in the console, everything works perfectly. Any idea why it's not functioning correctly in the script file?