Lodash _.rest() method
What you’ll learn
- How
_.restpacks trailing call arguments into one array. - Why the default
starttracksfunc.length - 1. - When lodash helps versus native rest parameters or
arguments. - Pitfalls around arity hints, default params, and manual start offsets.
Prerequisites
See _.rearg(), _.partial(), and the Function hub.
- Variadic intuition: functions that accept “the rest” after fixed headers.
- Function.length: how engines count declared parameters (legacy arity).
Overview
_.rest(func, [start]) wraps func so arguments from start onward collapse into a single array argument—classic lodash sugar before rest parameters were ubiquitous.
Syntax
_.rest(func, [start])- func: target whose trailing args should bundle.
- start: optional index where grouping begins (defaults to
func.length - 1). - returns: wrapped invoker producing the grouped call.
Default rest on the final parameter
With arity two, lodash defaults start to 1, so every argument after the prefix becomes an array.
import rest from "lodash/rest";
const greetMany = rest(function (prefix, names) {
return prefix + ": " + names.join(", ");
});
greetMany("Team", "Ada", "Grace", "Alan");
// "Team: Ada, Grace, Alan"Custom start index
Pass start = 2 when two leading scalars stay separate and everything else forms a tag bucket.
import rest from "lodash/rest";
const labelSku = rest(function (brand, sku, tags) {
return brand + "/" + sku + " [" + tags.join("|") + "]";
}, 2);
labelSku("ACME", "404", "sale", "featured", "new");
// "ACME/404 [sale|featured|new]"Head value plus numeric tail
Fold extras after the first argument—handy for CLI-style parsers.
import rest from "lodash/rest";
const sumHeadTail = rest(function (head, nums) {
return head + nums.reduce((acc, n) => acc + n, 0);
});
sumHeadTail(100, 1, 2, 3); // 106📋 _.rest vs native rest params vs arguments
| Approach | Strength | Trade-off |
|---|---|---|
_.rest | Retrofits existing callbacks without edits | Another wrapper layer |
Native ...rest | Zero-cost idiomatic ES2015+ | Requires changing the source signature |
arguments slicing | Works in legacy ES5 | Verbose and easy to mishandle strict mode edge cases |
Pitfalls to avoid
func.length surprises
Default parameters and destructuring can report smaller lengths—set start explicitly when metadata lies.
Passing one array argument
rest groups discrete invocation arguments; it does not auto-expand an array you passed as a single value unless you pair with spread.
Double wrapping
Layering rest, partial, and bind changes arity math—test merged wrappers with realistic call shapes.
❓ FAQ
Summary
- Purpose:
_.restbundles trailing invocation arguments for functions expecting an array tail. - Default: grouping begins at
func.length - 1unless you overridestart. - Next: Lodash _.spread(), revisit Lodash _.rearg(), or read official _.rest docs.
Lodash picks up invocation arguments starting at start (default func.length - 1) and forwards them as a single trailing array—mirroring ES2015 rest parameters for engines or signatures that predate native syntax.
6 people found this page helpful
