JavaScript React Components Managing State and Data Flow Update State Based on a Player's Index

Francois Martin
Francois Martin
1,958 Points

I don't understand where the index number comes from, it looks like its a cycle that is never set?

where does the index get set?

Daniel Nakonieczny
Daniel Nakonieczny
Pro Student 54,657 Points

The JavaScript map function passes it automatically, we were just ignoring/not using it before:

this.state.players.map( (player) =>

and now:

this.state.players.map( (player, index) =>

Take a look at the map documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

I believe the map function takes an optional index parameter corresponding to the index of the array it is mapping. So the players array has an implicit index based on its position in the array, which is then passed down.

Joshua Lucas
Joshua Lucas
8,764 Points

The index is the position of the player in the array. (I.e Guil's index is 0 because it is the first item in the array. )