Two Ways To Use Map() On An Array In Reverse Order In JavaScript

Use map() on an Array in Reverse Order in JavaScript

In JavaScript, we often use the map() method to create a new array from another array by getting the result of a function on the original array. However, how to use map() on an array in reverse order in JavaScript? Here are some solutions for you.

Use map() on an array in reverse order in JavaScript

You can refer to the following solutions to use map() on an array in reverse order in JavaScript.

Create an inverted array of the original array

To use map() on an array in reverse order in JavaScript, we need to use the reverse() method to reverse the array in JS.

Syntax

Arr.reverse()

Parameters: None.

Arr is the original array that we want to reverse. This method returns a new array without deleting the original array. We can use the map() method on the returned array without affecting the original array.

Example:

// define original array
var originalArray = ["Hello", "ABC","This is a test"] 

 // reverse array
var reverseArray = originalArray.reverse()

var result = reverseArray.map(function(i) {
    return i.toUpperCase()
})

// print the result
for ( i of Result) {
    console.log(i)
}

Output

THIS IS A TEST
ABC
HELLO

In this example, we use map() to return an array of strings in reverse of the original array while converting all strings in the array to uppercase.

Call a function inside the map() method

This tutorial will use the function inside the map() method to use map() on an array in reverse order in JavaScript. Specifically, we use the index parameter on the side of the function to process the array in the opposite direction.

Syntax:

array.map(function(currentValue, index, arr), thisValue)

Parameters: 

  • currentValue: value of the current element.
  • index: index of the current element.
  • arr: array of current element.
  • thisValue: the parameter passed from outside may or may not be. The default is undefined.

To use map() on arrays in reverse order in JavaScript instead of incrementing index from 0 to array.length-1 (length of the array), we would subtract the index from the length of the array to get the element’s index in the opposite direction.

Example:

// define original array
var originalArray = [1,2,3,4,5]

// get length of originalArray
var length = originalArray.length; 
var thisValue = 0;

// use map() to return an array original and multiplied by 5
var Result = originalArray.map(function(value, index, array) {
    return array[length - 1 - index]*5
}, thisValue)

for(i of Result) { 

// print Result
    console.log(i)
}

Output

25
20
15
10
5

The above code prints out a new array made up of the elements of the original array and multiplied by 5.

Summary

So, to use map() on an array in reverse order in JavaScript, we have shown 2 ways in the above article. These instructions are pretty simple. Please learn more to understand it better. Thanks for reading!

Maybe you are interested:

Leave a Reply

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