如何通过 API ID 重新排序对自定义订单的 API 调用?
How to reorder an API call to a custom order by API ID?
有点小众的问题,但对我提出了挑战,看看你能不能解决。
我有一系列体育联盟,它们按各自的 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 时它遵循随机顺序。我在数组方面相当弱,所以我如何让它按照我想要的顺序进行?
额外信息:
这是 console.log 的 API 调用,如您所见,联赛 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 (I think idk though)
我有你的解决方案,但有一个问题:你到达的 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 未返回所有所需数据),但它保持所需的顺序。
有点小众的问题,但对我提出了挑战,看看你能不能解决。
我有一系列体育联盟,它们按各自的 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 时它遵循随机顺序。我在数组方面相当弱,所以我如何让它按照我想要的顺序进行?
额外信息:
这是 console.log 的 API 调用,如您所见,联赛 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 (I think idk though)
我有你的解决方案,但有一个问题:你到达的 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 未返回所有所需数据),但它保持所需的顺序。