Async.waterfall,foreach和parsingJSON node.js. 需要一些build议

我的任务是parsing最后100个dota2游戏。 我从jiiin( https://github.com/jiin/dota2api )使用dota 2 API和小型lib,

我所有的,这是一个async.waterfall调用。 喜欢这个:

exports.get = function(req, res, next) { var playerID = +req.params.id; var playerCounter = []; var playerInfo = { kills: [], deaths: [], assists: [], last_hits: [], denies: [], hero_damage: [], hero_healing: [], gold_spent: [], kills_number: 0, deaths_number: 0, assists_number: 0 }; async.waterfall([ function getDota2Json(callback) { dota.getByAccountID(playerID, function (err, result) { callback(err, result); }); }, function getMatches(result, callback) { result.matches.forEach(function (match) { callback(null, match.match_id); }); }, function getMatchInfo(matchID, callback) { dota.getMatchDetails(matchID, function (err, result) { callback(err, result.players); }); }, function getCurrentPlayer(players, callback) { players.forEach(function (player) { if (player.account_id === playerID) { callback(null, player); } }); }, function getDamage(player, callback) { callback(null, player.kills, player.deaths, player.assists, player.last_hits, player.denies, player.hero_damage, player.hero_healing, player.gold_spent, '1'); } ], function (err, kills, deaths, assists, last_hits, denies, hero_damage, hero_healing, gold_spent, counter) { playerCounter.push(counter); playerInfo.kills.push(kills); playerInfo.deaths.push(deaths); playerInfo.assists.push(assists); console.log(playerCounter.length); if (playerCounter.length === 100) { playerInfo.kills.forEach(function (val) { playerInfo.kills_number += val; }); console.log('Last 100 K ' + playerInfo.kills_number); playerInfo.deaths.forEach(function (val) { playerInfo.deaths_number += val; }); console.log('Last 100 D ' + playerInfo.deaths_number); playerInfo.assists.forEach(function (val) { playerInfo.assists_number += val; }); console.log('Last 100 A ' + playerInfo.assists_number); var magickOpts = [ "-background", "grey60", "-bordercolor", "snow", "-border", "6", "-fill", "black", "-pointsize", "50", "label: Dota 2 LAST 100 \n K - D - A " + playerInfo.kills_number + ' - ' + playerInfo.deaths_number + ' - ' + playerInfo.assists_number, ""+playerID+".png" ]; var im = spawn('convert', magickOpts); } }); res.end('rdy'); 

};

我认为这不是很好的代码,但我找不到替代scheme。 Dota API使我在JSON中玩了100场比赛,但是我需要为game_id“forEach”获取详细信息,而为player.id中的“forEach”获取request.params.id中当前玩家的详细信息。 毕竟,我再次使用forEach来获取摘要数据。 这是正确的方式?)或者我愚蠢?)

可能你有一些想法做得更快? 可能需要使用MongoDB(?)或其他的东西。

最后我用统计生成简单的图像。

谢谢!

我觉得你可能可以使用async.each来简化一些forEach调用…

这可能是一个小挑逗,但要得到一个数组的数组,你可以使用数组的reduce方法。

 playerInfo.kills_number = playerInfo.kills.reduce(function(a, b) { return a + b; });