javascript - Angular - 将 HTTPS 请求结果合并到同一个数组中
我正在处理来自两个不同端点的两个 HTTPS 请求,并且我使用 forkJoin 来合并这两个结果。我遇到的困难是如何将数据从“info”传递到对应的“standings”中的团队,因为“info”包含每个足球队的更详细信息。
我的服务:
espStandings() {
return this.httpClient.get<Leagueinfo>('http://site.web.api.espn.com/apis/v2/sports/soccer/esp.1/standings?desc');
}
espInfo() {
return this.httpClient.get<Teaminfo>('https://site.api.espn.com/apis/site/v2/sports/soccer/esp.1/teams');
}
public getEsp() {
return forkJoin(this.espStandings(), this.espInfo()).pipe(map(([standings, info]) => {
return { standings, info };
}));
}
Standings 返回一个包含所有球队的数组,如下所示(这是 entries 数组中的第一支球队):
entries: Array(20)
0:
note: {color: "#81D6AC", description: "Champions League", rank: 1}
stats: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
team: {id: "3747", uid: "s:600~t:3747", location: "Granada", name: "Granada", abbreviation: "GCF", …}
我想做的是将一个名为“record”的新对象添加到“entries”中的每个团队中,并用“info”的数据填充它。
来自“info”结果的“record”对象看起来像这样:
record:
items: Array(1)
0:
stats: (22) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
所以每个团队的最终输出将是:
entries: Array(20)
0:
note: {color: "#81D6AC", description: "Champions League", rank: 1}
stats: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
team: {id: "3747", uid: "s:600~t:3747", location: "Granada", name: "Granada", abbreviation: "GCF", …}
record: {items: Array(1)}
排名的 JSON 示例:
{
"uid": "s:600~l:740",
"id": "15",
"name": "Spanish Primera División",
"abbreviation": "La Liga",
"children": [
{
"uid": "s:600~l:740~g:1",
"id": "1",
"name": "2019-20 LaLiga",
"abbreviation": "2019-2020",
"standings": {
"id": "0",
"name": "overall",
"displayName": "Overall Standings",
"links": [
{
"language": "en",
"rel": [
"standings",
"desktop"
],
"href": "http://www.espn.com/spanish-primera-division/15/table",
"text": "Table",
"shortText": "Table",
"isExternal": false,
"isPremium": false
}
],
"season": 2019,
"seasonType": 1,
"entries": [
{
"team": {
"id": "3747",
"uid": "s:600~t:3747",
"location": "Granada",
"name": "Granada",
"abbreviation": "GCF",
"displayName": "Granada",
"shortDisplayName": "Granada",
"isActive": true,
"logos": [
{
"href": "https://a.espncdn.com/i/teamlogos/soccer/500/3747.png",
"width": 500,
"height": 500,
"alt": "",
"rel": [
"full",
"default"
]
}
],
"links": [
{
"language": "en",
"rel": [
"clubhouse",
"desktop",
"team"
],
"href": "http://www.espn.com/football/club/_/id/3747/granada",
"text": "Clubhouse",
"shortText": "Clubhouse",
"isExternal": false,
"isPremium": false
}
]
},
"note": {
"color": "#81D6AC",
"description": "Champions League",
"rank": 1
},
"stats": [
{
}
]
信息的 JSON 示例:
{
"sports": [
{
"id": "600",
"uid": "s:600",
"name": "Football",
"slug": "soccer",
"leagues": [
{
"id": "740",
"uid": "s:600~l:740",
"name": "Spanish Primera División",
"abbreviation": "La Liga",
"shortName": "La Liga",
"slug": "spanish-primera-division",
"teams": [
{
"team": {
"id": "96",
"uid": "s:600~t:96",
"slug": "alaves",
"location": "Alavés",
"name": "Alavés",
"abbreviation": "ALV",
"displayName": "Alavés",
"shortDisplayName": "Alavés",
"color": "0000ff",
"alternateColor": "C8142F",
"isActive": true,
"isAllStar": false,
"logos": [
{
"href": "https://a.espncdn.com/i/teamlogos/soccer/500/96.png",
"width": 500,
"height": 500,
"alt": "",
"rel": [
"full",
"default"
]
},
{
"href": "https://a.espncdn.com/i/teamlogos/soccer/500-dark/96.png",
"width": 500,
"height": 500,
"alt": "",
"rel": [
"full",
"dark"
]
}
],
"record": {
"items": [
{
"summary": "1-2-2",
"stats": []
}
希望有人能指引我正确的方向。