如何使用filter()函数从API中过滤出这组数据?
How can I use the filter() function to filter this set of data from API?
API调用代码:
const settings = {
"async": true,
"crossDomain": true,
"url": "https://v3.football.api-sports.io/fixtures?date=2021-04-07",
"method": "GET",
"headers": {
"X-RapidAPI-Host": "v3.football.api-sports.io",
"X-RapidAPI-Key": "MY-API-KEY"
}
};
$.ajax(settings).done(function (response) { console.log(response)}); // Logs API Data, Need to Filter This
API数据的格式为:
response: Array(305) [ {…}, {…}, {…}, … ]
[0…99]
//FIRST ELEMENT
0: Object { fixture: {…}, league: {…}, teams: {…}, … }
fixture: Object { id: 812523, timezone: "UTC", date: "2022-05-13T00:00:00+00:00", … }
goals: Object { home: null, away: null }
league: Object { id: 395, name: "Divizia A", country: "Moldova", … }
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
teams: Object { home: {…}, away: {…} }
<prototype>: Object { … }
//SECOND ELEMENT
1: Object { fixture: {…}, league: {…}, teams: {…}, … }
fixture: Object { id: 830985, referee: "H. Prado", timezone: "UTC", … }
goals: Object { home: 4, away: 0 }
league: Object { id: 344, name: "Primera División", country: "Bolivia", … }
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
teams: Object { home: {…}, away: {…} }
<prototype>: Object { … }
示例中只显示了 0 和 1,但有 305 个元素。
我遇到的问题是我无法使用 by response[element] 在 for 循环中过滤那些 305。league.id(395 和 344 是上面的两个值)因为它不起作用。
假设我有一组我想要的联赛 ID(即 const arrWant=[395, 43, 308]),我该如何从数据中过滤掉我不需要的那些?我知道我必须使用 filter() 但不确定如何使用。如果有人能写出粗略的代码或函数,那将会很有帮助。Picture of API Output
使用includes()
方法测试响应中的联赛ID是否在arrWant
数组中。
使用 dataType: 'json'
以便 $.ajax()
解析响应中的 JSON。
const settings = {
"async": true,
"crossDomain": true,
"url": "https://v3.football.api-sports.io/fixtures?date=2021-04-07",
"method": "GET",
dataType: 'json',
"headers": {
"X-RapidAPI-Host": "v3.football.api-sports.io",
"X-RapidAPI-Key": "MY-API-KEY"
}
};
const arrWant=[395, 43, 308]
$.ajax(settings).done(function(data) {
console.log(data.response.filter(el => arrWant.includes(el.league.id)))
});
API调用代码:
const settings = {
"async": true,
"crossDomain": true,
"url": "https://v3.football.api-sports.io/fixtures?date=2021-04-07",
"method": "GET",
"headers": {
"X-RapidAPI-Host": "v3.football.api-sports.io",
"X-RapidAPI-Key": "MY-API-KEY"
}
};
$.ajax(settings).done(function (response) { console.log(response)}); // Logs API Data, Need to Filter This
API数据的格式为:
response: Array(305) [ {…}, {…}, {…}, … ]
[0…99]
//FIRST ELEMENT
0: Object { fixture: {…}, league: {…}, teams: {…}, … }
fixture: Object { id: 812523, timezone: "UTC", date: "2022-05-13T00:00:00+00:00", … }
goals: Object { home: null, away: null }
league: Object { id: 395, name: "Divizia A", country: "Moldova", … }
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
teams: Object { home: {…}, away: {…} }
<prototype>: Object { … }
//SECOND ELEMENT
1: Object { fixture: {…}, league: {…}, teams: {…}, … }
fixture: Object { id: 830985, referee: "H. Prado", timezone: "UTC", … }
goals: Object { home: 4, away: 0 }
league: Object { id: 344, name: "Primera División", country: "Bolivia", … }
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
teams: Object { home: {…}, away: {…} }
<prototype>: Object { … }
示例中只显示了 0 和 1,但有 305 个元素。
我遇到的问题是我无法使用 by response[element] 在 for 循环中过滤那些 305。league.id(395 和 344 是上面的两个值)因为它不起作用。
假设我有一组我想要的联赛 ID(即 const arrWant=[395, 43, 308]),我该如何从数据中过滤掉我不需要的那些?我知道我必须使用 filter() 但不确定如何使用。如果有人能写出粗略的代码或函数,那将会很有帮助。Picture of API Output
使用includes()
方法测试响应中的联赛ID是否在arrWant
数组中。
使用 dataType: 'json'
以便 $.ajax()
解析响应中的 JSON。
const settings = {
"async": true,
"crossDomain": true,
"url": "https://v3.football.api-sports.io/fixtures?date=2021-04-07",
"method": "GET",
dataType: 'json',
"headers": {
"X-RapidAPI-Host": "v3.football.api-sports.io",
"X-RapidAPI-Key": "MY-API-KEY"
}
};
const arrWant=[395, 43, 308]
$.ajax(settings).done(function(data) {
console.log(data.response.filter(el => arrWant.includes(el.league.id)))
});