Use map On An Array In Reverse Order In JavaScript

To use map on an array in reverse order in JavaScript, you can use any of the following methods –

  1. Use the arr.slice().reverse() method before calling the map method.
  2. Use the […arr].reverse() method before calling the map method.
  3. Use the Array.from(arr).reverse() method before calling the map method.

Let’s take a look at each of these methods in detail.

Use map On An Array In Reverse Order In JavaScript

use map on an array in reverse order in javascript

The arr.slice().reverse() method

This method creates a copy of the array, reverses it and then calls the map method on it.

var arr = ["a", "b", "c"];

var mappedArr = arr.slice().reverse().map(function(item) {

  console.log(item);

});

The mappedArr will contain the following values –

“c”

“b”

“a”

As you can see, the array is first reversed and then the map method is called on it.

The arr.slice() method creates a copy of the array, which is then reversed using the reverse() method.

The map method is called on the reversed array and it prints out all the items in the array.

The […arr].reverse() method

Spread operator can be used to reverse the array. This method is similar to arr.slice().reverse() method because it creates a copy of the array before reversing it, so that the original array is not affected.

var arr = ["a", "b", "c"];

var mappedArr = […arr].reverse().map(function(item) {

  console.log(item);

});

The mappedArr will contain the following values –

“c”

“b”

“a”

As you can see, the array is first reversed and then the map method is called on it.

The […arr] creates a copy of the array, which is then reversed using the reverse() method.

The map method is called on the reversed array and it prints out all the items in the array.

The Array.from(arr).reverse() method

This method uses the built-in Array.from() method to create a new array from the passed array. Then reverse() method is called on it which reverses the new array. Finally, map() method is called on the reversed array.

var arr = ["a", "b", "c"];

var mappedArr = Array.from(arr).reverse().map(function(item) {

  console.log(item);

});

The mappedArr will contain the following values –

“c”

“b”

“a”

As you can see, the array is first reversed and then the map method is called on it.

The Array.from() method creates a new array from the passed array. Then reverse() method is called on it which reverses the new array.

The map method is called on the reversed array and it prints out all the items in the array.

Conclusion

In this article, we saw how to use map on an array in reverse order in JavaScript. We saw three different methods to do it – using the arr.slice().reverse() method, using the […arr].reverse() method and using the Array.from(arr).reverse() method.

All these methods create a copy of the array before reversing it, so that the original array is not affected by the operation.

Hope this article was helpful in understanding how to use map() on an array in reverse order in JavaScript. Happy coding! 🙂

Leave a Reply