14/16 What tells i to incrememnt


#1

Apologies if this has been posed before, but I’m confused by this exercise.

I wanted to know the value of i that was making up a unique key, so I ran my own code to actually display the value of i, and noticed that it was essentially just an integer that incremented for each list item, so it appeared as
‘Rowe1’
‘Prevost2’
‘Gare3’

I just wanted to know what is it that causes the value to automatically increment, since ‘i’ is not declared anywhere else in the code.

Just for my understanding, and help would be appreciated.

Thanks


#2

Please post a link to the exercise.

If there is a loop, then i is declared in the loop parameters.


#3

This is the final result for the exercise.

import React from ‘react’;
import ReactDOM from ‘react-dom’;

const people = [‘Rowe’, ‘Prevost’, ‘Gare’];

const peopleLis = people.map((person, i) =>
// expression goes here:

  • {person}
  • );

    // ReactDOM.render goes here:
    ReactDOM.render(

      {peopleLis}
    , document.getElementById(‘app’));

    #4

    https://www.codecademy.com/courses/react-101/lessons/react-jsx-advanced/exercises/jsx-keys?action=resume_content_item


    #5

    map() is an iterator that returns a new list mapped over and computed from the old list

    const peopleLis = people.map((person, i) =>
      // expression goes here:
      <li key={'person_' + i}></li>
    );
    
    // ReactDOM.render goes here:
    ReactDOM.render(<ul>{peopleLis}</ul>, document.getElementById('app'));
    

    i (the second parameter) is a placeholder for the internal index kept by map().


    #6

    This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.