I looked at similar questions and tried Object.key(patientList).length but it returns 0. I was recommended to use callbacks and this how I am implementing it.
var data;
var patientList = {};
var parseDate = d3.time.format("%d/%m/%y").parse;
function input_Data() {
d3.json("data.php", function(error, json) {
if (error) return console.warn(error);
data = json;
console.log(data);
for(var i = 0; i < data.length; i++) {
var name = data[i].name;
//data[i].dates = parseDate(data[i].dates);
if(!patientList[name]) {
var newPatient = {
dates: [data[i].dates],
alpha: data[i].alpha,
beta: data[i].beta
};
patientList[name] = newPatient;
} else {
patientList[name].dates.push(data[i].dates);
}
}
console.log(patientList);
console.log(Object.keys(patientList).length);
console.log(Object.keys(patientList));
});
}
function number_of_patients(callback) {
callback();
console.log(patientList);
console.log(Object.keys(patientList).length);
console.log(Object.keys(patientList));
}
number_of_patients(input_Data);
console.log inside the input_Data function displays correct results with length 4. And console.log in the number_of_patients displays correct patientList but 0 length and doesn't display name (keys) either. I have read similar posts but still can't fix the problem here.
Any help would be much appreciated.