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 reverse array?

How to reverse array, or map with descending order?

<tbody>
  {Array(10).fill(1).map((el, i) =>
    <ObjectRow key={i} />
  )}
</tbody> 

reverse function does not work

<tbody>
  {Array(10).fill(1).reverse().map((el, i) =>
    <ObjectRow key={i} />
  )}
</tbody> 

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

>Solution :

You have 2 mistakes here.

First you load an array with 1s, which means that you have identical elements inside your array, so reverse is pointless.

Second you use the index as a key inside your map method and not the actual reversed element (using index as key is not considered best practice).

The correct way should be

<tbody>
  { [...Array(10).keys()].reverse().map((el, i) =>
    <ObjectRow key={el} />
  )}
</tbody> 
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