How to use the nth function from lodash

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

lodash.nth is a function that returns the nth element of an array, counting from either the beginning or the end of the array, depending on the index value provided.

276
277
278
279
280
281
282
283
284
285
module.exports.neq                 = _.neq;
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;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

567
568
569
570
571
572
573
574
575
576
{
  name: 'array.get',
  benchmarks: () => ({
    iiris: () => A.get(0, num100),
    ramda: () => R.nth(0, num100),
    lodash: () => _.nth(num100, 0),
    native: () => num100?.[0],
  }),
},
{
fork icon1
star icon31
watch icon0

How does lodash.nth work?

lodash.nth works by taking an array as its first argument, and a number (the index) as its second argument. The index argument determines which element of the array will be returned. A negative index can also be used to count backwards from the end of the array. If the index is out of range (greater than or equal to the length of the array, or less than the negative length of the array), the function returns undefined. Here is the syntax for lodash.nth: c Copy code {{{{{{{ class="!whitespace-pre hljs language-c">_.nth(array, [n=0]) And here is a breakdown of the arguments: array (Array): The array to query. [n=0] (number): The index of the element to return. Here's an example of how lodash.nth can be used to return the nth element of an array: javascript Copy code {{{{{{{ const _ = require('lodash'); const arr = ['a', 'b', 'c', 'd']; console.log(_.nth(arr, 1)); // Output: 'b' console.log(_.nth(arr, -2)); // Output: 'c' In this example, _.nth(arr, 1) returns the second element of the arr array ('b'), while _.nth(arr, -2) returns the third-to-last element of the arr array ('c').

84
85
86
87
88
89
90
91
92
93
94
95
96
console.log(last); // => 3


const lastIndexOf = _.lastIndexOf([1, 2, 1, 2], 2);
console.log(lastIndexOf); // => 3


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


const pull = _.pull(['a', 'b', 'c', 'a', 'b', 'c'], 'a', 'c');
console.log(pull); // => ['b', 'b']
fork icon0
star icon4
watch icon0

+ 15 other calls in file

991
992
993
994
995
996
997
998
999
{
  var objectSchema = jp.get(swagger, _.dropRight(path, 3));

  if (value) {
    objectSchema.required = objectSchema.required || [];
    var propertyName = _.nth(path, -2);
    if (objectSchema.required.indexOf(propertyName) === -1)
      objectSchema.required.push(propertyName);
  }
fork icon0
star icon0
watch icon5

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("lodash");

const arr = ["apple", "banana", "cherry", "date", "elderberry"];

console.log(_.nth(arr, 2));
// Output: 'cherry'

console.log(_.nth(arr, -1));
// Output: 'elderberry'

In this example, we create an array of fruits and pass it to _.nth along with the index value of 2. This returns the third element of the array ('cherry'). We also pass the index value of -1, which returns the last element of the array ('elderberry').

32
33
34
35
36
37
38
39
40

// Remove the 'arn:' part
trimmed = trimmed.substring('arn:'.length);

// Get the partition part
const partition = _.nth(_.split(trimmed, ':'), 0);

// Check if it is still an s3 arn
if (!_.startsWith(trimmed, `${partition}:s3:::`)) return;
fork icon0
star icon0
watch icon1

+ 7 other calls in file

258
259
260
261
262
263
264
265
266
267
268
//_.nth(array,[n=0])
let nthArr = ['a', 'b', 'c', 'd'];
let nth1 = _.nth(nthArr, 1);
console.log('nth1--->', nth1);
//nth1---> b
let nth2 = _.nth(nthArr, -2);
console.log('nth2--->', nth2);
//nth2---> c


//_.pull(array,[values])
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

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