Last updated: Mar 4, 2024
Reading timeยท5 min
If you need to use
map()
on an array in reverse order in React.js, click on the second subheading.
To use the map()
method on an array in reverse order:
slice()
method to get a copy of the array.reverse()
method to reverse the copied array.map()
method on the reversed array.const arr = ['a', 'b', 'c']; const mapReverse1 = arr .slice(0) .reverse() .map(element => { return element; }); console.log(mapReverse1); // ๐๏ธ ['c', 'b', 'a']
The first step is to use the Array.slice() method to create a shallow copy of the array.
We do this because the Array.reverse() method changes the contents of the original array in place.
The only argument we passed to the Array.slice()
method is the start
index - the index of the first element to be included in the new array.
By specifying a start index of 0
and no end index, we create a shallow
copy of the original array, which we can reverse.
const arr = ['a', 'b', 'c']; const copy = arr.slice(0); console.log(copy); // ๐๏ธ ['a', 'b', 'c']
The Array.reverse()
method reverses an array in place and returns the result.
const arr = ['a', 'b', 'c']; const reversed = arr.reverse(); console.log(reversed); // ๐๏ธ ['c', 'b', 'a'] console.log(arr); // ๐๏ธ ['c', 'b', 'a']
arr
variable was also reversed.This is the reason we created a shallow copy in advance - to avoid changing the original array.
The last step is to use the Array.map() method on the reversed array.
const arr = ['a', 'b', 'c']; const mapReverse1 = arr .slice(0) .reverse() .map(element => { return element; }); console.log(mapReverse1); // ๐๏ธ ['c', 'b', 'a']
The function we passed to the Array.map() method gets called with each element in the array.
The map()
method returns a new array containing the values returned from the
callback function.
An alternative approach is to use the spread syntax (...) to create a shallow copy of the array.
This is a three-step process:
reverse()
method to reverse the copied array.map()
method on the reversed array.const arr = ['a', 'b', 'c']; const mapReverse2 = [...arr].reverse().map(element => { return element; }); console.log(mapReverse2); // ๐๏ธ ['c', 'b', 'a']
The spread syntax (...) unpacks the values of the original array into a new array, creating a shallow copy.
We then reverse the copy to avoid mutating the original array and call the
map()
method on the reversed array.
This approach is a bit more concise than using the slice()
method.
You can also use map()
on an array in reverse order by using the current
index.
const arr = ['a', 'b', 'c']; const mapReverse = arr.map((_element, index) => { return arr[arr.length - 1 - index]; }); console.log(mapReverse); // ๐๏ธ ['c', 'b', 'a']
The second argument the callback function gets called with is the index of the current iteration.
1
and the current index from the array's length to iterate over the array in reverse order.For example, on the first iteration, we get an index of arr.length - 1 - 0
.
For an array that has 3
, elements this evaluates to 2
, which is the last
index in the array.
JavaScript indexes are zero-based, so the first element in an array has an index
of 0
and the last element has an index of array.length - 1
.
On each iteration, we subtract 1
and the current index from the array's length
to map over the array in reverse order.
An alternative approach is to use Array.map()
to iterate over the array from
index 0
onwards and then reverse the new array.
const arr = ['a', 'b', 'c']; const mapReverse = arr .map(element => { return element; }) .reverse(); console.log(mapReverse); // ๐๏ธ [ 'c', 'b', 'a' ]
The code sample calls the Array.map()
method on the array directly.
The map()
method returns a new array, so we can safely call the reverse()
method on the array without mutating the original.
reduceRight
You can also use the Array.reduceRight() method to iterate over an array in reverse order.
const arr = ['a', 'b', 'c']; const result = arr.reduceRight((accumulator, last) => { return accumulator.concat(last); }, []); console.log(result); // ๐๏ธ [ 'c', 'b', 'a' ]
The reduceRight()
method applies the provided function against an accumulator
and each value of the array from right to left, reducing the array to a single
value.
The accumulator
variable is initialized to an empty array because that's what
we passed as the second argument to the Array.reduceRight()
method.
To reverse an array in React:
reverse()
method on the copy.export default function App() { const people = [ {name: 'Alice', country: 'Austria'}, {name: 'Bob', country: 'Belgium'}, {name: 'Carl', country: 'Canada'}, ]; // ๐๏ธ create copy and reverse const reversed = [...people].reverse(); return ( <div> {reversed.map((person, index) => { return ( <div key={index}> <h2>Name: {person.name}</h2> <h2>Country: {person.country}</h2> <hr /> </div> ); })} </div> ); }
We used the spread syntax (...) to unpack the values of the array into a new array, creating a shallow copy.
reverse()
method without modifying the original array.The Array.reverse() method changes the contents of the original array in place.
const arr = ['a', 'b', 'c']; const reversed = arr.reverse(); console.log(reversed); // ๐๏ธ ['c', 'b', 'a'] console.log(arr); // ๐๏ธ ['c', 'b', 'a']
The reverse()
method reverses an array in place and returns the result. This
might not be the behavior you're looking for.
arr
variable was also reversed.This is the reason we created a shallow copy in advance - to avoid changing the original array.
Alternatively, you can use the Array.slice() method.
slice()
This is a three-step process:
slice()
method on the array to create a shallow copy.reverse()
method on the copy.export default function App() { const people = [ {name: 'Alice', country: 'Austria'}, {name: 'Bob', country: 'Belgium'}, {name: 'Carl', country: 'Canada'}, ]; // ๐๏ธ create copy and reverse const reversed = people.slice().reverse(); return ( <div> {reversed.map((person, index) => { return ( <div key={index}> <h2>Name: {person.name}</h2> <h2>Country: {person.country}</h2> <hr /> </div> ); })} </div> ); }
The first step is to create a shallow copy of the array by using the Array.slice() method.
const arr = ['a', 'b', 'c']; const copy = arr.slice(); console.log(copy); // ๐๏ธ ['a', 'b', 'c']
When the slice
method is called without supplying any parameters, it returns a
shallow copy of the original array.
Then we can call the reverse()
method on the copy to avoid mutating the
original array.
Which approach you pick is a matter of personal preference. I'd go with the
spread syntax (...) because it's more readable and intuitive, especially if the
reader of your code is not familiar with the parameters the slice()
method
takes.