Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,148 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
216,148 Points

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