1

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: 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!

kmoser
  • 8,780
  • 3
  • 24
  • 40

1 Answers1

0

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.

JCollier
  • 1,102
  • 2
  • 19
  • 31
  • 1
    Thank you so much! I combined your answer with some of the comments above and I managed to get it working. Literally celebrating right now ahaha – Ethan Carlton Mar 07 '23 at 14:37