Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Map function on an array not working in React

I’m new to ReactJS. Somehow, I’m unable to get proper output using map on array.

I have an array named pets.

  const pets = [{
    name: 'Bruno',
    type: 'Dog',
    description: 'Bruno is funny and lovable',
    skills: ['fetch', 'being cute', 'roll over'],
    image: 'https://www.dogbreedinfo.com/images17/LabmaranerCasey6Weeks1.JPG'
  },
  {
    name: 'Rocky',
    type: 'Dog',
    description: 'Rocky is funny and lovable',
    skills: ['bite', 'being cute', 'roll over'],
    image: 'https://st.depositphotos.com/1000291/3041/i/950/depositphotos_30415145-stock-photo-white-labrador-puppy.jpg'
  }];

And I’m trying to display using map as below.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  <div>
    <h1>My pets</h1>
    <ul>
      {
        pets.map((pet, index) => {             
          <li key={index}>
            <h1> {pet.name} </h1>
          </li>
        })
      }
    </ul>

  </div>

Doing this is not displaying any data from the array.

What is wrong with my code , can someone tell me?

>Solution :

yes, you forgot to return, you can check Implicit vs Explicit return here

you are doing explicit return, so it’s required to write return keyword yourself… as

{
    pets.map((pet, index) => {             
      return(<li key={index}>
        <h1> {pet.name} </h1>
      </li>)
    })
  }

or turn in to implicit as below:

{
    pets.map((pet, index) =>(             
        <li key={index}>
          <h1> {pet.name} </h1>
        </li>
      )
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading