How to use the reverse() method to reverse an array In Lodash
In this approach, we are using Lodash’s reverse method directly on an array to reverse its elements, modifying the original array, and the res variable will store the reversed array.
Syntax:
_.reverse(array)
Example: The below example uses the lodash’s reverse() method to reverse an array.
const _ = require('lodash');
const arr = [1, 2, 3, 4, 5];
const res = _.reverse(arr);
console.log(res);
Output:
[ 5, 4, 3, 2, 1 ]
How to use the Reverse method in Lodash ?
The Reverse method in Lodash is the go-to function for quickly reversing arrays or strings, offering a convenient and efficient way to manipulate data structures in JavaScript.
The below approaches will explain how you can use the reverse method in Lodash.
Table of Content
- Using the reverse() method to reverse an array
- Using the reverse() method to reverse a string
Run the below command before running the below codes in your local:
npm i lodash