eachRight()
eachRight<
T>(collection,iteratee):void
Iterates over elements of collection from right to left.
DEPRECATED
Use [...array].reverse().forEach() directly instead.
Type Parametersβ
T: Tβ
The type of elements in the collection.
Parametersβ
collection: readonly T[]β
The collection to iterate over.
iteratee: (value, index, collection) => voidβ
The function invoked per iteration.
Returns: voidβ
Sinceβ
See Alsoβ
Array.forEach() - MDN 2.0.0
Also known asβ
forEachRight (Lodash, es-toolkit) Β· β (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleβ
// β Deprecated approach
eachRight([1, 2, 3], x => console.log(x));
// logs: 3, 2, 1
// β
Recommended approach
[...[1, 2, 3]].reverse().forEach(x => console.log(x));
// logs: 3, 2, 1
How it works?β
Iterates over elements from right to left.
Deprecated: Use array.reverse().forEach() or loop backward.
Native Equivalentβ
// β eachRight(arr, fn)
// β
[...arr].reverse().forEach(fn)
// β
for (let i = arr.length - 1; i >= 0; i--) fn(arr[i])
Use Casesβ
Iterate in reverse πβ
Process array elements from end to start.
const items = [1, 2, 3];
[...items].reverse().forEach(item => console.log(item));
// => 3, 2, 1
Process stack orderβ
Handle items in LIFO order.
const stack = ["first", "second", "third"];
for (let i = stack.length - 1; i >= 0; i--) {
process(stack[i]);
}
Undo in reverse orderβ
Apply undo operations in reverse.
const history = [action1, action2, action3];
history.reduceRight((_, action) => action.undo(), null);