如何遍历数据中的数组:AJAX调用

时间:2021-05-01 15:40:33

I have an array of courses. I'm using Rapid API for the back-end. I want to loop through the array and equate if "course": [one of those elements in the array].

我有一系列的课程。我正在使用Rapid API作为后端。我想循环遍历数组并等于“course”:[数组中的其中一个元素]。

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() {
/* ... */
});

I know I'm way off, but this is where I've got till.

我知道我已经离开了,但这是我要到的地方。

2 个解决方案

#1


0  

To evaluate if specific value present in your array or not you could use anonymous IIFE(Wiki). It will be something like this:

要评估数组中是否存在特定值,您可以使用匿名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() {

  });

#2


0  

The way to loop through arrays is basically

循环遍历数组的方法基本上就是这样

.done(function(data, textStatus, jqXHR) { data.each(function(item) { if (item.courseTitle == 'blah') { console.log(item); } }); console.log(data); })

.done(function(data,textStatus,jqXHR){data.each(function(item){if(item.courseTitle =='blah'){console.log(item);}}); console.log(data) ;})

#1


0  

To evaluate if specific value present in your array or not you could use anonymous IIFE(Wiki). It will be something like this:

要评估数组中是否存在特定值,您可以使用匿名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() {

  });

#2


0  

The way to loop through arrays is basically

循环遍历数组的方法基本上就是这样

.done(function(data, textStatus, jqXHR) { data.each(function(item) { if (item.courseTitle == 'blah') { console.log(item); } }); console.log(data); })

.done(function(data,textStatus,jqXHR){data.each(function(item){if(item.courseTitle =='blah'){console.log(item);}}); console.log(data) ;})