Next.js Discord

Discord Forum

Request: To rewrite the map function [Chapter 6 from Next JS Tutorial]

Answered
English Angora posted this in #help-forum
Open in Discord
English AngoraOP
Hi I am new to JS and just learnt map function. I am trying to rewrite the following code taken from chapter 6 (https://nextjs.org/learn/react-foundations/displaying-data-with-props). I intend to remove the '=>' notation. Can you please help identify the mistake in my code? Thanks


Working code from tutorial

function HomePage() {
const names = ['Ada Lovelace', 'Grace Hopper', 'Margaret Hamilton'];

return (
<div>
<Header title="Develop. Preview. Ship." />
<ul>
{names.map((name) => (
<li>{name}</li>
))}
</ul>
</div>
);
}

My code that needs to be fixed

function HomePage() {
const names = ['A', 'B', 'C', 'D'];
return (
<div>
<Header title = "Develop. Preview. Ship."></Header>
<ul>
{
names.map(function (namesElement))
{
return (
<li> {namesElement} </li>
)};
}
</ul>
</div>
);
}
Answered by Arinji
 {
                                names.map( function (name, index) {
                                    return <li key={index}>{name}</li>
                                }) 
                            }
View full answer

6 Replies

@English Angora
 {
                                names.map( function (name, index) {
                                    return <li key={index}>{name}</li>
                                }) 
                            }
Answer
you forgot the function key word
also dont put a () in your return if its just a one liner (this isnt an error, just clean code)
English AngoraOP
Thank you for the help and the tip, it worked
@English Angora Thank you for the help and the tip, it worked
no worries, mark a solution :D