【发布时间】:2020-03-17 04:29:21
【问题描述】:
目前,我在函数外部初始化了一个名为约会的列表,我的代码正在从该函数内将对象推送到该数组中。
var appointments = [];
async function getAllAppointmentEvents() {
var doctorList = await getAllDoctors();
// For all doctors
for (let i = 1; i < doctorList["doctorinfo"].length; i++) {
// Find their appointments
schedule = await getAppointmentsByDoctor(i).then(function (response) { return response; });
// For each appointment
if (schedule["schedules"] !== undefined) {
for (let j = 0; j < schedule["schedules"].length; j++) {
var startDate = new Date(schedule["schedules"][j]["datetime"]);
// startDate = new Intl.DateTimeFormat("")
var endDate = new Date(schedule["schedules"][j]["datetime"]);
endDate.setHours(startDate.getHours() + 1);
var result = {
title: doctorList["doctorinfo"][i][1],
start: startDate.toISOString().slice(0, 19),
end: endDate.toISOString().slice(0, 19),
color: "#C0C0C0",
groupId: doctorList["doctorinfo"][i][0]
};
appointments.push(result);
}
}
}
}
当我 console.log 输出时,我得到一个似乎包含所有对象的空数组。但我无法通过appointments[0] 访问这些对象。我也试过打印出Object.keys(appointments),它也没有任何键。当我在函数内使用 console.log 时,可以正常访问该数组。
当我从函数外部 console.log 列表时得到的输出:output
我需要帮助从函数外部访问对象。谢谢!
【问题讨论】:
-
您需要发布代码以尝试解决问题。
-
发布您的代码,以便有机会快速回答。
-
嗨!您是指整个文件吗?
-
你在哪里声明
appointments? -
我在函数之外声明了约会!我已经更新了代码以显示这一点
标签: javascript jquery arrays ajax object