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) {
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.
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.
'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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With