I am encountering an issue where I am creating an array of objects and then trying to access object properties within the array, but it keeps coming back as undefined. After calling the createObjArray() function, a console.log(objArray[1]) prints out the object with all its properties just fine. However, when I try to console.log(objArray[1].name), Firebug shows "undefined". Even when stepping through my code in Firebug, hovering over objArray[1].name displays the correct name. This situation is really frustrating me.
var objArray = [];
function createObjectArray(numOfObjs) {
for(var i=0; i<numOfObjs; i++) {
packages.push(initObj(i));
}
}
function initObj(i){
var newPackage;
var p = {};
$.getJSON('.../package' + i + '.json', function(data) {
newPackage = new Package(data);
p.name = newPackage.name;
p.id = i;
});
return p;
}