我一直致力于一个网页的功能,该网页应该显示来自JSON对象的信息,具体取决于两个属性,即出勤日和开始时间。 JSON和函数如下如下:
var courses = [{
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "8:00",
"End_Time": "9:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "10:30",
"End_Time": "11:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "12:30",
"End_Time": "1:30",
"Attendance": "Wednesday"
}];
function plotCourse() {
var i;
for (i = 0; i < courses.length; i++) {
var course = courses[i];
console.log(course);
if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
alert(course.Attendance);
alert(course.Start_Time);
}
}
}
我正在使用alert()进行测试。但是,我似乎没有得到返回值,我有点难过,可能会发生什么。
提前致谢。
答案 0 :(得分:2)
您忘了给它打电话:plotCourse();
,或者使用 IIFE 。请检查一下
var courses = [{
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "8:00",
"End_Time": "9:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "10:30",
"End_Time": "11:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "12:30",
"End_Time": "1:30",
"Attendance": "Wednesday"
}];
(function() {
var i;
for (i = 0; i < courses.length; i++) {
var course = courses[i];
console.log(course);
if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
alert(course.Attendance);
alert(course.Start_Time);
}
}
})(); /* IIFE*/
答案 1 :(得分:1)
希望你在调用这个函数。
在这里工作:
var courses = [{
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "8:00",
"End_Time": "9:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "10:30",
"End_Time": "11:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "12:30",
"End_Time": "1:30",
"Attendance": "Wednesday"
}];
function plotCourse() {
var i;
for (i = 0; i < courses.length; i++) {
var course = courses[i];
console.log(course);
if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
console.log(course.Attendance);
console.log(course.Start_Time);
}
}
}
plotCourse(); //Involed the function
答案 2 :(得分:1)
您忘了拨打plotCourse()
功能。
var courses = [{
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "8:00",
"End_Time": "9:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "10:30",
"End_Time": "11:30",
"Attendance": "Monday"
}, {
"dept": "CSC",
"id": "3380",
"instructor": "Douglas",
"Location": "012 Lockett",
"Start_Time": "12:30",
"End_Time": "1:30",
"Attendance": "Wednesday"
}];
function plotCourse() {
var i;
for (i = 0; i < courses.length; i++) {
var course = courses[i];
console.log(course);
if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
alert(course.Attendance);
alert(course.Start_Time);
}
}
}
plotCourse();
答案 3 :(得分:0)
您提出的示例不是被调用的函数。 iniltre我对 FOR 循环进行了小修正.. =)
function plotCourse() {
for (var i = 0; courses[i]; i++) { // Control if the element exists
var course = courses[i];
console.log(course);
if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
alert(course.Attendance);
alert(course.Start_Time);
}
}
}
plotCourse(); // launch the function!
答案 4 :(得分:0)
正如其他人已经说过的那样,你需要调用这个函数。当我打电话给它时,它对我有用。