我有一个包含许多具有唯一ID的游戏的数组。我需要为每个id提出请求,以检索为特定游戏播放的分钟数。
所以我的数组看起来像这样:
[
{
id: 1,
name: 'Team 1 vs Team 2'
},
{
id: 2,
name: 'Team 1 vs Team 2'
},
{
id: 3,
name: 'Team 1 vs Team 2'
},
{
id: 4,
name: 'Team 1 vs Team 2'
},
{
id: 5,
name: 'Team 1 vs Team 2'
}
]
我一直在尝试通过数组,通过Axios向API发出请求,然后检索数据。
Api看起来像这样。它还有更多,只是缩小了范围。下面有一张图片,内容更多:
{
event: {
123123: {
id: 1,
elapsed: {
1: {
elapsed: '45'
}
}
}
}
由于某种原因,已用完的键是唯一的,所以我使用的是Object.keys来查找"它
我的代码:
array.forEach(game => {
axios.get(`http://example.com/${game.id}`)
.then(res => {
let time = 0;
Object.keys(res.data.event).map(eventId) => { //the eventId is the id for the event, in this case 123123.
Object.keys(res.data.event[eventId].elapsed).map(el => {
time = res.data.event[eventId].elapsed[el].elapsed; //el is the key-id for the elapsed object.
)};
)};
return time;
});
});
这不起作用,我只是未定义。我究竟做错了什么?是的,API看起来像一团糟。我有以下图片以防万一。它是一个JSON文件,我只有一个JSON Formatter:
编辑:编辑我的代码