反向 for 循环的 lodash 等价物是什么?

What is the lodash equivalent for a reverse for loop?

我正在尝试找出如何在 lodash 中重新创建这种类型的反向 for 循环:

此处将首先迭代数组的最后一项:

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

对于常规 for 循环:

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

我可以用 lodash 的 _.each:

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

Lodash 有 forEachRight().


没有lodash,你可以调用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()

请记住,这实际上会在您的代码中添加另一个迭代。如果这对性能至关重要,则向后计数 for 循环是个好主意。