Syntax, Parameter and Return Value
Syntax:
_.nthArg([n=0])
Parameters:
[n=0]: The index of the argument to return, with negative indexing from the end (e.g., -1 for the last argument).
Return Value:
(Function) - Returns the new pass-thru function.
Examples
Accessing a Specific Argument:
JavaScript
var _ = require('lodash');
var getSecondArg = _.nthArg(1);
console.log(getSecondArg('a', 'b', 'c'));
You can also try this code with Online Javascript Compiler
Run Code
Output:
'b'
Demonstrates creating a function that returns the second argument passed to it.
Using with Higher-Order Functions:
JavaScript
var numbers = ['one', 'two', 'three'];
var getThird = _.nthArg(2);
var thirdItems = numbers.map(getThird);
console.log(thirdItems);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[undefined, undefined, 'three']
Shows using _.nthArg() in a map function to get the third argument (index) of each element.
Accessing the Last Argument:
JavaScript
var getLastArg = _.nthArg(-1);
console.log(getLastArg(1, 2, 3, 4, 5));
You can also try this code with Online Javascript Compiler
Run Code
Output:
5
An example of using _.nthArg() to access the last argument passed to a function.
Combining with Other Lodash Functions:
JavaScript
var _ = require('lodash');
var complexFunction = _.flow([_.add, _.nthArg(-1), _.multiply(2)]);
console.log(complexFunction(5, 3));
You can also try this code with Online Javascript Compiler
Run Code
Output:
6 ((5 + 3), then 3, then 3 * 2)
Demonstrates combining _.nthArg() with other Lodash functions to create a more complex operation.
Frequently Asked Questions
How does _.nthArg() handle out-of-bounds indices?
If the specified index is out of bounds (i.e., the function doesn't receive enough arguments), _.nthArg() will return undefined.
Can _.nthArg() be used with functions that have a variable number of arguments?
Yes, _.nthArg() works well with variadic functions, allowing you to extract a specific argument regardless of the total number of arguments passed.
Is _.nthArg() commonly used in real-world applications?
_.nthArg() is particularly useful in functional programming patterns and situations where operations on specific arguments are required repeatedly across different functions.
Conclusion
Lodash's _.nthArg() method offers a concise and effective way to create functions that return a specific argument from the list of arguments passed. It's a valuable tool in scenarios that require manipulation or retrieval of specific arguments, enhancing the flexibility and readability of functional programming in JavaScript.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.