There's a different way to loop through an array. I think it's pretty cool. For example:
['π', 'π', 'π₯'].map(function(fruit) { return 'I like ' + fruit; })
Do you know what's going on here?
- First of all,
['π', 'π', 'π₯']
is an array of strings. - Second,
.map()
means it'll call the functionmap()
but with the specific data in the array of fruits. - Third, the thing inside
map()
is a function, right?
This code will loop through the array of fruits, call the function with each fruit, and assemble the return values into a new array, in their respective order.
If you paste the code above into Chrome's console and press return, you'll get this:
['I like π', 'I like π', 'I like π₯']
This way of looping through an array is really useful in React. Take a mental note and we'll come back to it!