Skip to main content

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