Lodash _.ary() method
What you’ll learn
- How
_.ary(func, n)drops extra parameters before callingfunc. - Why pairing
arywithparseIntfixes surprisingmapoutput. - How default arity and the optional
guardargument behave. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional reads Function hub and _.after(); know that map passes (value, index) into callbacks.
- Function arity: how many parameters a function expects (
fn.lengthin simple cases). parseInt(string, radix): second argument is radix—easy to confuse with map indices.
Overview
_.ary(func, n) builds a thin wrapper that invokes func with at most n arguments—extras from callers such as map, event handlers, or timers are ignored. That keeps utilities like parseInt from accidentally consuming metadata arguments.
Iteratee safety
Higher-order APIs often forward three arguments; ary trims them down to what your function truly needs.
Configurable cap
Pick any arity ceiling—not limited to unary helpers.
Tree-shakeable
Import lodash/ary when wrapping is all you need.
Syntax
_.ary(func, [n = func.length], [guard]) - func: target function to wrap (must be callable).
- n: maximum arguments forwarded; omitted or nullish defaults to
func.lengthunlessguardresets the branch. - guard: when truthy, Lodash ignores the provided
nand usesfunc.lengthfor the cap. - Returns: a wrapped function preserving
thisfor the inner call with truncated arguments.
parseInt with _.map
Official Lodash example: cap parseInt to one argument so each string parses in base 10 instead of inheriting the numeric index as radix.
import ary from "lodash/ary";
import map from "lodash/map";
map(["6", "8", "10"], ary(parseInt, 1));
// [6, 8, 10] Native map(parseInt) footgun
Contrasting the same input without ary: indices leak into parseInt’s radix slot, producing NaN or unexpected bases.
["6", "8", "10"].map(parseInt);
// [6, NaN, 2] — index 2 makes radix 2 for "10" Cap a multi-argument function
Lodash forwards only the first two operands—extra trailing junk from a loose caller never reaches the reducer.
import ary from "lodash/ary";
const addAll = (a, b, c) => a + b + (c ?? 0);
const addTwo = ary(addAll, 2);
addTwo(1, 2, 999);
// 3 📋 _.ary vs _.unary
| Helper | Arity | When to reach for it |
|---|---|---|
_.ary(func, n) | Any cap you choose | Binary parsers, dual-arg reducers, testing harnesses |
_.unary(func) | Always 1 | Shorthand for event handlers and map iteratees needing only the first slot |
Both reuse the same internal wrapping machinery; pick unary when brevity beats an explicit number.
Pitfalls to avoid
Too small n
Dropping arguments you still need yields silent logic bugs—verify against real caller signatures.
func.length quirks
Rest parameters and native builtins may report 0 or misleading lengths; set n explicitly when unsure.
Still pass radix?
Even with ary(parseInt, 1), specify 10 when parsing arbitrary user strings—this helper only fixes the map arity issue.
❓ FAQ
Summary
- Purpose:
_.ary(func, n)forwards at mostnarguments tofunc. - Classic fix: combine with
parseIntinsidemapto ignore indices. - Next: Lodash _.before(), _.after(), or official Lodash docs for _.ary.
Lodash documents _.unary(func) as arity-1 sugar—in practice it is implemented as _.ary(func, 1), so anything unary does you can express explicitly with ary.
6 people found this page helpful
