diff --git a/snippets/nthArg.md b/snippets/nthArg.md new file mode 100644 index 000000000..c94224326 --- /dev/null +++ b/snippets/nthArg.md @@ -0,0 +1,17 @@ +### nthArg + +Creates a function that gets the argument at index `n`. If `n` is negative, the nth argument from the end is returned. + +Use `Array.slice()` to get the desired argument at index `n`. + +```js +const nthArg = n => (...args) => args.slice(n)[0]; +``` + +```js +const third = nthArg(2); +third(1,2,3); // 3 +third(1,2); // undefined +const last = nthArg(-1); +last(1,2,3,4,5); // 5 +``` diff --git a/tag_database b/tag_database index 8d46a58d6..10dace2a4 100644 --- a/tag_database +++ b/tag_database @@ -136,6 +136,7 @@ merge:object,array minBy:math,array,function minN:array,math negate:function +nthArg:utility,function nthElement:array objectFromPairs:object,array objectToPairs:object,array