HOME > lodash

[lodash] Reverse the order of an array, using the reverse function

Last Modified : 2023-03-05 / Created : 2023-03-06
View Count

Let's learn about the lodash method reverse, which reverses the order of an array.


The reverse() method of lodash is a useful method that reverses the order of an array as its name suggests. For example, if you want to reverse the order of the array myNum below, you can simply use reverse().
let myNum = [1, 2, 3];
reverseNum = _.reverse(myNum);

The execution result of the code is as follows.

// Result
[3, 2, 1]

I simply changed the order in reverse, as you can see.

! Using the JavaScript reverse() function., vanillaJS
By the way, it is also possible to use the built-in function reverse() in JavaScript. If we implement it in the same way, it would be like the following.
let myNum = [1, 2, 3];
reverseNum = myNum.reverse();

// Result
[3, 2, 1]

As you can see from the results, it is the same as the reverse() above.

We have learned about the reverse() function in lodash up to this point.


Post thumbnail [lodash] How to use the trim() function and examples


Merge with unique values using lodash union method Post thumbnail