Skip to main content

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]