How to use the nthArg function from lodash

Find comprehensive JavaScript lodash.nthArg code examples handpicked from public code repositorys.

lodash.nthArg returns a function which returns the nth argument passed to it.

277
278
279
280
281
282
283
284
285
286
module.exports.noConflict          = _.noConflict;
module.exports.noop                = _.noop;
module.exports.not                 = _.not;
module.exports.now                 = _.now;
module.exports.nth                 = _.nth;
module.exports.nthArg              = _.nthArg;
module.exports.nths                = _.nths;
module.exports.omit                = _.omit;
module.exports.omitBy              = _.omitBy;
module.exports.omitWhen            = _.omitWhen;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

929
930
931
932
933
934
935
936
937
938
939
940
941
console.log(noConflict); // => undefined


const noop = _.noop();
console.log(noop); // => undefined


const nthArg = _.nthArg(1);
console.log(nthArg('a', 'b', 'c', 'd')); // => 'b'


const over = _.over([Math.max, Math.min]);
console.log(over(1, 2, 3, 4)); // => [4, 1]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.nthArg work?

lodash.nthArg is a lodash utility function that creates a new function that returns the nth argument passed to it. The n argument specifies the position of the argument to return. If n is negative, the nth argument from the end will be returned. If n is greater than or equal to the number of arguments passed, undefined is returned. This function is useful in situations where you need to get a specific argument from a function, regardless of how many arguments were passed to it.

Ai Example

1
2
3
4
5
6
7
8
const { nthArg } = require("lodash");

function foo(a, b, c) {
  const secondArg = nthArg(1)(a, b, c);
  console.log(secondArg);
}

foo(1, 2, 3); // Output: 2

In this example, we import the nthArg function from the lodash library. We define a foo function that takes three arguments, but we only use nthArg to get the second argument and log it to the console. We call the foo function with three arguments 1, 2, and 3, and it logs the second argument, which is 2.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)