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]