英文:
How do I get the name of the players and the data contained inside the players?
问题
I'm new to Javascript and jQuery and I'm trying to make an in game scoreboard for Rocket League and ran into some trouble.
Here is the console log of data
from the game:
I'm trying to access the stats like boost
contained inside the players, however, but I don't really want to change the player's names in the code every time to access it.
I was able to access the team scores by doing this
$(".scorebug .team.left .score").text(d['game']['teams'][0]['score']);
but for the players, I tried doing something like
$(".scorebug .playernames .playerBlue1").text(d['players'].child());
and
$(".scorebug .playernames .playerBlue1").text(d['players'].next());
but it doesn't work.
Is there a way I can select Buzz_2
, Foamer_5
, Heater_6
, Outlaw_1
, Sundown_3
, and Tex_7
without having to type their names in the code every time? Thanks!
英文:
I'm new to Javascript and jQuery and I'm trying to make an in game scoreboard for Rocket League and ran into some trouble.
Here is the console log of data
from the game:
I'm trying to access the stats like boost
contained inside the players, however, but I don't really want to change the player's names in the code every time to access it.
I was able to access the team scores by doing this
$(".scorebug .team.left .score").text(d['game']['teams'][0]['score']);
but for the players, I tried doing something like
$(".scorebug .playernames .playerBlue1").text(d['players'].child());
and
$(".scorebug .playernames .playerBlue1").text(d['players'].next());
but it doesn't work.
Is there a way I can select Buzz_2
, Foamer_5
, Heater_6
, Outlaw_1
, Sundown_3
and Tex_7
without having to type their names in the code every time? Thanks!
答案1
得分: 0
如果您只想按数据中的顺序获取球员的统计信息,您可以使用JavaScript的for...in
循环来遍历它们。
您想要对这些分数做什么?您想要将它们记录到控制台吗?还是想要将它们保存到变量或数组以供以后使用?
如果您只想将它们记录下来,您可以这样简单做:
for (let player in data.players) {
let playerBoostStat = data.players[player].boost;
console.log(player + ' has a boost of ' + playerBoostStat);
}
这将记录一个句子,其中包括每个球员的姓名和相应的提升统计信息。
英文:
If you just want to get the players stats in the order they appear in the data, you can simply loop through them with JavaScript's for...in
loop.
What do you want to do with these scores? Do you want to log them to your console? Or do you want to save them to variables or an array for later use?
If you just want to log them, you could do something as simple as
for (let player in data.players) {
let playerBoostStat = data.players[player].boost
console.log(player + ' has a boost of ' + playerBoostStat)
}
That will log a sentence exclaiming each player's name and respective boost stat.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论