Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lodash equivalent for a reverse for loop?

I'm trying to figure out how to recreate this type of reverse for loop in lodash:

Here the last item of the Array will be iterated on first:

for (var j=quotes.data[data_location].length-1; j>=0; j--)

For the regular for loop:

for (var j=0; j < quotes.data[data_location].length; j++)

I can accomplish this with lodash's _.each:

_.each(quotes.data[data_location], function(quote) {

like image 323
Leon Gaban Avatar asked Apr 12 '16 14:04

Leon Gaban


People also ask

How do I reverse an array in Lodash?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. reverse() is used to reverse the array. This function changes the original array and also returns a new array.

Is Lodash equal?

isEqual() Method. The Lodash _. isEqual() Method performs a deep comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.

Is Uniq a Lodash?

'uniq' The Lodash uniq method creates an array without duplicate values. Only the first occurrence of each value appears in the returned array. Since JavaScript already has the set data structure, we can use that with the spread operator to remove all the duplicates from the original array and return it.

What is _ get?

Overview. The _. get() method in Lodash retrieves the object's value at a specific path. If the value is not present at the object's specific path, it will be resolved as undefined . This method will return the default value if specified in such a case.


Video Answer


1 Answers

Lodash has forEachRight().


Without lodash, you could call reverse() on the loop first, which will mutate the original array. If you don't want to do this, you can create a shallow copy of the array with slice().

Keep in mind that this will essentially add another iteration in your code. If this is performance critical, a backwards counting for loop is a good idea.

like image 149
alex Avatar answered Sep 22 '22 10:09

alex