有點小眾的問題,但它挑戰了我,看看你能不能解決它。
我有這一系列的體育聯盟按各自的 API ID 從最好到最差進行排名。
const arrOrderedLeague = [1, 2, 3, 4, 5, 6, 7, 9, 10, 29, 30, 31, 32, 33,
34, 39, 45, 48, 140, 142, 135, 137, 78, 81, 61, 65, 66, 88,
94, 96, 253, 203, 262, 179, 185, 144, 188, 169, 40, 41, 42,
43, 235, 207, 218, 141, 136,333, 307, 197, 62, 79, 80, 128,
130, 292, 98, 101, 103, 106, 113, 119, 283, 71, 73, 265, 239, 211, 89 ]
盡管數字 89 低于 211,但 211 是更好的聯賽。
問題是當我呼叫 API 時,它遵循隨機順序。在陣列方面我相當弱,所以我將如何讓它按照我想要的順序?
額外資訊:
這是 API 呼叫的 console.log,如您所見,聯賽 73 在 262 之前記錄,但由于聯賽 262 在我的陣列中早于 73,我希望它提前記錄,這可能需要創建一個新陣列有一些我不知道的功能,但我不確定它是什么。
2: Object { fixture: {…}, league: {…}, teams: {…}, … }
??
fixture: Object { id: 854430, referee: "Savio Pereira Sampaio, Brazil", timezone: "UTC", … }
??
goals: Object { home: 3, away: 0 }
??
league: Object { **id: 73**, name: "Copa Do Brasil", country: "Brazil", … }
??
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
??
teams: Object { home: {…}, away: {…} }
??
<prototype>: Object { … }
3: Object { fixture: {…}, league: {…}, teams: {…}, … }
??
fixture: Object { id: 861506, referee: "Jorge Antonio Perez Duran, Mexico", timezone: "UTC", … }
??
goals: Object { home: 1, away: 2 }
??
league: Object { **id: 262**, name: "Liga MX", country: "Mexico", … }
??
score: Object { halftime: {…}, fulltime: {…}, extratime: {…}, … }
??
teams: Object { home: {…}, away: {…} }
??
<prototype>: Object { … }
?
不太需要,但這里是 API 呼叫本身:
const settings = {
"async": true,
"crossDomain": true,
"url": "https://v3.football.api-sports.io/fixtures?date=2022-05-13",
"method": "GET",
dataType: 'json',
"headers": {
"X-RapidAPI-Host": "v3.football.api-sports.io",
"X-RapidAPI-Key": "API-KEY"
}
};
$.ajax(settings).done(function (data) {
const newArr = data.response.filter(el => arrWant.includes(el.league.id));
console.log(newArr)
});
如果您需要任何澄清,請發表評論,感謝任何嘗試過的人!
Json Of Response(雖然我認為是 idk)
uj5u.com熱心網友回復:
我有你的解決方案,但有一個問題:你到達的 API 端點沒有回傳你想要的所有聯賽。它回傳 302 項,但其中只有 16 項與您指定的 69 個聯賽 ID 中的一些匹配。
盡管如此,為了達到您想要的順序,您可以:
- 獲取資料
- 映射所需的 ID,從 API 回應中回傳相應的專案。如果不存在,該欄位將是未定義的
- 過濾掉所有未定義的值
const desiredOrder = [
1, 2, 3, 4, 5, 6, 7, 9, 10, 29, 30, 31, 32, 33, 34, 39, 45, 48, 140, 142, 135, 137, 78, 81, 61, 65, 66, 88, 94, 96, 253, 203, 262, 179, 185,
144, 188, 169, 40, 41, 42, 43, 235, 207, 218, 141, 136, 333, 307, 197, 62, 79, 80, 128, 130, 292, 98, 101, 103, 106, 113, 119, 283, 71, 73,
265, 239, 211, 89,
];
const run = async () => {
// Make the request
const res = await fetch('https://v3.football.api-sports.io/fixtures?date=2022-05-13', {
headers: {
'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
// Take my API key idc
'X-RapidAPI-Key': '9e4b7483aa00fa65f63f96c52c184754',
},
});
// Convert response body to JSON and grab the "response" property (array)
const json = (await res.json())?.response;
// Loop through the desired IDs and for each return the corresponding API response item
const ordered = desiredOrder.map((id) => json.find(({ league }) => league?.id === id));
// Remove duplicate (undefined) values to shorten array, then filter out all undefineds
const filtered = [...new Set(ordered)].filter(item => item !== undefined)
console.log(filtered);
};
run();
盡管此陣列與desiredOrder陣列的長度不同(由于 API 未回傳所有所需的資料),但它保持所需的順序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473777.html
標籤:javascript html jQuery api
