Categories
JavaScript Answers

How to use map() on an array in reverse order with JavaScript?

Spread the love

To use map() on an array in reverse order with JavaScript, we make a copy before we reverse the array.

For instance, we write

myArray
  .slice(0)
  .reverse()
  .map((a) => {
    //...
  });

to call slice to make a copy of myArray and return it.

And then we call reverse to reverse the copied array.

Then we call map to map the entries of the reversed array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *