Lodash _.forEachRight() method
What you’ll learn
- How
_.forEachRight(collection, iteratee)differs only in traversal direction from_.forEach. - Tail-first patterns for stacked undo buffers, trailing-sentinel scans, or preserving last-writer expectations.
- When manual
reverseduplicates work—and when Lodash keeps iteratee ergonomics simpler. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.forEach() first—the iteratee signature and return semantics are identical.
- You can reason about zero-based indexes from the end of an array.
- You can open Try-it labs or run snippets locally.
Overview
_.forEachRight is the mirrored cousin of _.forEach: same chaining story, same iteratee toolbox—only the visitation schedule runs backward on indexed collections.
Tail-first arrays
Great when the meaningful signal lives near the end—audit trails, version stacks, append logs.
Chain-ready
Returns the collection so fluent pipelines stay uninterrupted.
Exit friendly
Return false to bail once a backward scan finds what it needs.
Syntax
_.forEachRight(collection, iteratee) - collection: array, array-like, or plain object Lodash can iterate.
- iteratee: invoked as
(value, index|key, collection); indexes descend on arrays. - Returns: the original
collectionreference.
Capture visitation indexes from the tail
Push each visited index into an array—the descending pattern becomes obvious compared with forEach.
import forEachRight from "lodash/forEachRight";
const seen = [];
forEachRight(["a", "b", "c"], (_value, index) => {
seen.push(index);
});
seen;
// → [2, 1, 0] Concatenate values tail-first
String-building shows how later slots dominate ordering without reversing the source array.
import forEachRight from "lodash/forEachRight";
let acc = "";
forEachRight([10, 20, 30], (n) => {
acc += String(n);
});
acc;
// → "302010" Stop after two visits from the right
return false still halts the loop—here only the last two elements run before exit.
import forEachRight from "lodash/forEachRight";
const hits = [];
forEachRight([10, 5, 20], (value) => {
hits.push(value);
if (hits.length === 2) return false;
});
hits;
// → [20, 5] 📋 _.forEachRight vs forEach, reverse, findLast
| API | Traversal | Best when |
|---|---|---|
_.forEachRight(collection, iteratee) | Tail → head on arrays | Effects must observe trailing elements first |
_.forEach(collection, iteratee) | Head → tail | Default forward scans |
array.slice().reverse().forEach(...) | Reversed copy | When you explicitly need a reversed snapshot |
_.findLast(...) | First match from tail | You only need one element, not every callback |
Pitfalls to avoid
Key order myths
Do not assume object keys mirror array reverse semantics—verify against fixtures.
Still not async-aware
Await inside iteratee does not serialize magically—use explicit async flows.
Accidental O(n) work
If only the last element matters, findLast may avoid redundant callbacks.
❓ FAQ
Summary
- Purpose:
_.forEachRight(collection, iteratee)runs side-effect callbacks from the end on indexed collections. - Contrast: prefer
forEachwhen forward order reads clearer; preferfindLastwhen one hit suffices. - Next: Lodash _.groupBy(), Lodash _.forEach() (previous), or collection hub.
For arrays, indexes decrease from length - 1 down to 0. For plain objects, rely on Lodash’s documented enumeration rather than assuming insertion-order symmetry with forEach.
6 people found this page helpful
