how to use the eachright function from lodash in javascript


eachRight is a function from the Lodash library for iterating over elements of an array in reverse order. The method performs a specific action on each element in the array, such as updating or transforming it.

Here's an example of how to use eachRight:

index.tsx
const _ = require('lodash');

const array = [1, 2, 3, 4, 5];

_.eachRight(array, (element, index) => {
  console.log(`array[${index}] = ${element}`);
});
154 chars
8 lines

In the code above, we import the Lodash library using the require method. We also declare an array array with five elements.

The eachRight method is then called on the array with a callback function that takes two parameters: the current element and its index in the array. The callback function logs the current element and its index to the console.

When the above code is run, the eachRight method iterates through the array elements in reverse order, logging each element and its index to the console, resulting in the following output:

index.tsx
array[4] = 5
array[3] = 4
array[2] = 3
array[1] = 2
array[0] = 1
65 chars
6 lines

Note that eachRight returns the original array, so it does not create a new array.

gistlibby LogSnag