nthArg()
nthArg(
n): (...args) =>unknown
Creates a function that gets the argument at index n.
DEPRECATED
Use an inline arrow function (...args) => args[n] instead.
Parameters
n: number
The index of the argument to return.
Returns
A function that returns the nth argument.
Since
2.0.0
Also known as
nthArg (Lodash, es-toolkit, Ramda) · ❌ (Remeda, Radashi, Effect, Modern Dash, Antfu)
Example
// ❌ Deprecated approach
const getSecond = nthArg(1);
getSecond('a', 'b', 'c'); // => 'b'
// ✅ Recommended approach
const getSecond = (...args: string[]) => args[1];
getSecond('a', 'b', 'c'); // => 'b'
How it works?
Creates a function that gets the argument at index n. Deprecated: Use an arrow function.
Native Equivalent
// ❌ nthArg(1)
// ✅ (...args) => args[1]
Use Cases
Extract specific argument 📌
Create function that returns nth argument.
const getSecond = (...args: unknown[]) => args[1];
getSecond('a', 'b', 'c'); // 'b'
Callback adapter
Adapt callback to use specific argument.
// Get just the index from map callback
const getIndex = (_: unknown, i: number) => i;
['a', 'b', 'c'].map(getIndex); // [0, 1, 2]