Lodash _.reduceRight() method
What you’ll learn
- How
_.reduceRight(collection, iteratee, accumulator)mirrorsreducebut walks backward. - Contrast outputs when operations depend on evaluation order.
- How object visitation interacts with right folds.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.reduce() first—the iteratee contract is identical; only traversal direction changes.
- You understand commutative versus order-sensitive combining operators.
- You can open Try-it labs or run snippets locally.
Overview
_.reduceRight keeps Lodash iteratee ergonomics while honoring algorithms that naturally consume data from the tail—reverse polish vibes without reversing arrays manually.
Backward folds
Accumulator sees trailing elements first on arrays.
Same signature
Swap direction without rewriting callback plumbing.
Mind ordering
Addition might match reduce—but concatenation usually won’t.
Syntax
_.reduceRight(collection, iteratee, [accumulator]) - collection: array or plain object Lodash can iterate.
- iteratee:
(accumulator, value, index|key, collection); invoked from last element backward. - accumulator (optional): initial fold state—prefer explicit values when empties occur.
- Returns: final accumulator after visiting every element.
Push indexes onto an array tail-first
Compared with reduce, elements append in reverse visitation order.
import reduceRight from "lodash/reduceRight";
reduceRight([1, 2, 3], (acc, n) => [...acc, n], []);
// → [3, 2, 1]
// Versus reduce(...) → [1, 2, 3] Concatenate strings from right to left
Sequential concatenation is not commutative—direction decides the final literal.
import reduceRight from "lodash/reduceRight";
reduceRight(["a", "b", "c"], (acc, ch) => acc + ch, "");
// → "cba" Fold object values tail-first
Concatenate numeric coercions as strings—reverse visitation yields "321" rather than "123".
import reduceRight from "lodash/reduceRight";
reduceRight(
{ a: 1, b: 2, c: 3 },
(acc, value) => acc + String(value),
""
);
// → "321" 📋 _.reduceRight vs reduce, native reduceRight
| API | Traversal | Best when |
|---|---|---|
_.reduceRight(collection, iteratee, acc) | Tail → head | Algorithms needing trailing-first accumulation |
_.reduce(collection, iteratee, acc) | Head → tail | Default forward folds |
Array.prototype.reduceRight | Tail → head (arrays only) | Pure arrays without Lodash iteratee sugar |
Pitfalls to avoid
Looks commutative?
Addition/multiplication often hide direction—test with subtraction or string ops.
Spread chains
Spreading into new arrays each iteration is educational but allocations-heavy—mutate carefully when scaling.
Key expectations
Document expected enumeration—tie-break logic depends on engine-stable ordering.
❓ FAQ
Summary
- Purpose:
_.reduceRight(collection, iteratee, accumulator)folds while visiting elements from the end. - Contrast: identical math to
reduceonly when operations commute. - Next: Lodash _.reject(), Lodash _.reduce() (previous), or collection hub.
Use reduceRight when tail-first visitation defines correctness—string builders, subtract/divide chains, or prepend-heavy list constructions—not merely because data happens to look reversed.
6 people found this page helpful
