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

How to display text only once per item iterated in React map

I am trying to display text only once per iteration block, but it keeps displaying depending on how many indexed there are iterated.

How can I explain only once?

This is my code:

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

someArr.map((col, i) => {
  return (
    <div
      key={i}
    >
      {text} // should be displayed only ONCE
      {someRule === someOtherRule && (
        <Icon/>
      )}
   </div>
 )
})

So what happens here is that Icon and all other items get displayed correctly, but {text} gets displayed depending on how many there are iterations. How can I display it only once, regardless of iteration count?

>Solution :

Check for the first index

  {i === 0 ? text: ''} 
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