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);