我有一系列课程。我使用Rapid API作为后端。我想循环遍历数组并等同于"当然":[数组中的其中一个元素]。
sendEmail.js
var courses = ["course1", "course2", "course3", "course4"];
var courseLoop;
var dayLoop;
jQuery.ajax({
url: "http://something-something.rapidapi.io/get-day",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {
"course":for(i=0;i<4;i++){
courseLoop = courses[i];
if("course" == courseLoop){
alert("found");
break;
}
},
"day": "1",
},
})
.done(function(data, textStatus, jqXHR) {
alert("HTTP Request Succeeded: " + jqXHR.status);
console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert("HTTP Request Failed");
})
.always(function() {
/* ... */
});
我知道我已经离开了,但这是我要去的地方。
答案 0 :(得分:0)
循环遍历数组的方法基本上是
.done(function(data, textStatus, jqXHR) {
data.each(function(item) {
if (item.courseTitle == 'blah') {
console.log(item);
}
});
console.log(data);
})
答案 1 :(得分:0)
要评估数组中是否存在特定值,您可以使用匿名IIFE(Wiki)。 它将是这样的:
var courses = ["course1", "course2", "course3", "course4"];
var courseLoop;
var dayLoop;
jQuery.ajax({
url: "/echo/json/",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {
"course": (function(courses, courseLoop) {
//in jquery you can use inArray
for (var i = 0; i < 4; i++) {
if ("course" === courses[i]) {
courseLoop = courses[i];
//alert("found");//Removing Blocking Call
if (window.console && console.log) console.log('found');
break;
}
}
return courseLoop;
}(courses, courseLoop)), //Douglas Crockford's style
"day": "1"
}
})
.done(function(data, textStatus, jqXHR) {
//alert("HTTP Request Succeeded: " + jqXHR.status);
if (window.console && console.log) console.log(data, textStatus, jqXHR, jqXHR.status);
})
.fail(function(jqXHR, textStatus, errorThrown) {
//alert("HTTP Request Failed");
if (window.console && console.log) console.log(jqXHR, textStatus, errorThrown);
})
.always(function() {
});