[英]How do I convert line 6 and 7 from python into javascript?
我正在尝试将此功能从python转换为javascript。 但我不知道如何将第6行和第7行的for循环转换为javascript。
async def compute_wn8(stats_totals, exp_stat_totals) -> float:
"""Compute the WN8 of a player."""
wn8 = 0
if stats_totals and exp_stat_totals:
stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)
r_dmg = dmgs / exp_dmgs if exp_dmgs > 0 else 0
r_spot = spots / exp_spots if exp_spots > 0 else 0
r_kill = kills / exp_kills if exp_kills > 0 else 0
r_def = defs / exp_defs if exp_defs > 0 else 0
r_win = wins / exp_wins if exp_wins > 0 else 0
r_dmg_c = max(0., (r_dmg - 0.22) / 0.78)
r_spot_c = max(0., min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62))
r_kill_c = max(0., min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88))
r_def_c = max(0., min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90))
r_win_c = max(0., (r_win - 0.71) / 0.29)
wn8 += 980 * r_dmg_c
wn8 += 210 * r_dmg_c * r_kill_c
wn8 += 155 * r_kill_c * r_spot_c
wn8 += 75 * r_def_c * r_kill_c
wn8 += 145 * min(1.8, r_win_c)
return wn8
这是我走了多远:
async function compute_wn8(stats_totals, exp_stat_totals)
//Compute the WN8 of a player.
wn8 = 0
if(stats_totals && exp_stat_totals){
stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)
if(exp_dmgs>0) {r_dmg = dmgs / exp_dmgs} else r_dmg = 0;
if(xp_spots>0) {r_spot = spots / exp_spots} else xp_spots = 0;
if(exp_kills > 0) {r_kill = kills / exp_kills} else exp_kills = 0;
if(exp_defs > 0) {r_def = defs / exp_defs} else exp_defs = 0;
if(exp_wins > 0) {r_win = wins / exp_wins} else exp_wins = 0;
r_dmg_c = max(0., (r_dmg - 0.22) / 0.78)
r_spot_c = max(0., min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62))
r_kill_c = max(0., min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88))
r_def_c = max(0., min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90))
r_win_c = max(0., (r_win - 0.71) / 0.29)
wn8 += 980 * r_dmg_c
wn8 += 210 * r_dmg_c * r_kill_c
wn8 += 155 * r_kill_c * r_spot_c
wn8 += 75 * r_def_c * r_kill_c
wn8 += 145 * min(1.8, r_win_c)
return wn8
}
但我不知道如何将这两行转换为javascript:
dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)
我希望你们每个人都能帮助我:)
假设您的对象中有stat_totals
,则可以将python理解+解stat_totals
转换为javascript map
+分解:
let stats_totals = { dmgs:10, spots:20, kills:30, defs:40, wins:50} let stat_keys = ['dmgs', 'spots', 'kills', 'defs', 'wins'] // dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys) let [dmgs, spots, kills, defs, wins] = stat_keys.map(stat => stats_totals[stat] ) console.log(dmgs, spots, kills, defs, wins)
在给定对象exp_stat_totals
,另一行也应采用相同的想法。
您也许还可以直接分解对象:
let stats_totals = { dmgs:10, spots:20, kills:30, defs:40, wins:50} let {dmgs, spots, kills, defs, wins} = stats_totals console.log(dmgs, spots, kills, defs, wins)
过去,我已经将代码从一种语言转换为另一种语言。 您应该避免直接进行翻译,而应尝试理解代码的功能并进行翻译。 区分什么是逻辑,什么是该语言的特性或实现细节。
尽管Mark Meyer给出了将这些行从字面上转换为JS的答案,并且可以正常工作,但我还是建议不要使用该代码。 在JS中它既复杂又缓慢。
因为与Python理解这些理解不同,JS尚不足够聪明,无法理解stat_keys
是属性名称的静态列表,并且无法优化这些Aray#map调用,然后进行数组销毁。
这就是我翻译您的函数的方式:
// Compute the WN8 of a player.
// why is this function async in your code?
function compute_wn8(stats_totals, exp_stat_totals) {
if (!stats_totals || !exp_stat_totals) {
return 0;
}
const r_dmg = exp_stat_totals.dmgs > 0 ? stats_totals.dmgs / exp_stat_totals.dmgs : 0;
const r_spot = exp_stat_totals.spots > 0 ? stats_totals.spots / exp_stat_totals.spots : 0;
const r_kill = exp_stat_totals.kills > 0 ? stats_totals.kills / exp_stat_totals.kills : 0;
const r_def = exp_stat_totals.defs > 0 ? stats_totals.defs / exp_stat_totals.defs : 0;
const r_win = exp_stat_totals.wins > 0 ? stats_totals.wins / exp_stat_totals.wins : 0;
const r_dmg_c = Math.max(0, (r_dmg - 0.22) / 0.78);
const r_spot_c = Math.max(0, Math.min(r_dmg_c + 0.1, (r_spot - 0.38) / 0.62));
const r_kill_c = Math.max(0, Math.min(r_dmg_c + 0.2, (r_kill - 0.12) / 0.88));
const r_def_c = Math.max(0, Math.min(r_dmg_c + 0.1, (r_def - 0.10) / 0.90));
const r_win_c = Math.max(0, (r_win - 0.71) / 0.29);
return 980 * r_dmg_c
+ 210 * r_dmg_c * r_kill_c
+ 155 * r_spot_c * r_kill_c
+ 75 * r_def_c * r_kill_c
+ 145 * Math.min(1.8, r_win_c);
}
除此之外,即使在python中,我也不确定
stat_keys = ('dmgs', 'spots', 'kills', 'defs', 'wins')
dmgs, spots, kills, defs, wins = (stats_totals[stat] for stat in stat_keys)
exp_dmgs, exp_spots, exp_kills, exp_defs, exp_wins = (exp_stat_totals[stat] for stat in stat_keys)
r_dmg = dmgs / exp_dmgs if exp_dmgs > 0 else 0
r_spot = spots / exp_spots if exp_spots > 0 else 0
r_kill = kills / exp_kills if exp_kills > 0 else 0
r_def = defs / exp_defs if exp_defs > 0 else 0
r_win = wins / exp_wins if exp_wins > 0 else 0
比这更好(更短,更易读,更快) 。
r_dmg = stats_totals.dmgs / exp_stat_totals.dmgs if exp_stat_totals.dmgs > 0 else 0
r_spot = stats_totals.spots / exp_stat_totals.spots if exp_stat_totals.spots > 0 else 0
r_kill = stats_totals.kills / exp_stat_totals.kills if exp_stat_totals.kills > 0 else 0
r_def = stats_totals.defs / exp_stat_totals.defs if exp_stat_totals.defs > 0 else 0
r_win = stats_totals.wins / exp_stat_totals.wins if exp_stat_totals.wins > 0 else 0
并且,请声明您的变量并使用;
在JS中。 您未来的自我会感谢您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.