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!

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

Marcus Thelin
Marcus Thelin
1,188 Points

Where did we get the index value from?

For example when we do the

toggleConfirmationAt = indexToChange => {
    this.setState({
      guests: this.state.guests.map((guest, index) => {
        if(index === indexToChange) {
          return {
            ...guest,
            isConfirmed: !guest.isConfirmed
          }
        }
        return guest;
      })
    })
  }

1 Answer

Steven Parker
Steven Parker
228,025 Points

The map method supplies both the current item and it's index as arguments to the callback.

Marcus Thelin
Marcus Thelin
1,188 Points

Thank you so much! I have totally missed that. Here's a reference to the ppl with the same questions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Steven Parker
Steven Parker
228,025 Points

Or you can click on the blue word "map" in my answer. :wink: