我得到这段代码来从 Riot Games League of Legends API 中检索一批比赛:
const matches = await Promise.all(matchlist.matches.map(function(match){
return qRequest(server, "/match/v3/matches/" + match.gameId);
}));
返回值是一个 JSON 格式的字符串。所以 matches
最终成为一个 JSON 字符串数组。不过,我需要解析 JSON,以便匹配对象数组。
我只是想不通如何将 JSON.parse 放在这里?
最佳答案
可以申请Array#map
匹配
let matches = ... ;
matches = matches.map(json => JSON.parse(json));
或内联调用 JSON.parse
:
const matches = await Promise.all(matchlist.matches.map(function(match){
return qRequest(server, "/match/v3/matches/" + match.gameId)
.then(json => JSON.parse(json));
}));
关于javascript - 如何将 JSON.parse 放入 Promise.all?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774788/