With the following prompt and correct answer how is the first item in the "show_1"
Render a list of shoe items in an ordered list and set the key attribute for each item to equal
"shoe_"" and the current number in the list. for example the first item in the list should have the key "shoe_1"
const shoes = ['flip flops', 'basketball shoes', 'tennis shoes'];
const shoeList = shoes.map((shoe, i) => (<li key={"shoe_"+i}>{shoeList}</li>);
<ol>{shoeList}</ol>
I don’t understand how the first item is shoes_1 if an index i would start at 0.
2
Answers
The first element of a JavaScript array is not the 1st element, but the 0th element.
Hence, if the expected behavior you want is for your iteration to print the first 0th element as 1, you should be doing
i+1
.Edit:
You should never use the index (i.e
i
) of the mapping as a key.Since they item (i.e.
shoe
) is a string, just use it directly.