Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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?

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.

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. )

1 Answer

Daniel Nakonieczny
PLUS
Daniel Nakonieczny
Courses Plus 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