Thursday, October 16, 2025
HomeLanguagesJavascriptLodash _.third() Method

Lodash _.third() Method

The Lodash _.third() method takes an array and an index and hence returns an array generated by taking elements from the original array starting with the third element and ending at the given index.

Syntax:

_.third(array, index);

Parameters: This method takes two parameters as mentioned above and described below:

  • array: The given array from the elements are to be taken.
  • index: The index up to which elements are to be taken.

Return Value: This method returns a generated array.

Note: This will not work in normal JavaScript because it requires the lodash contrib library to be installed. 

Module Installation: Lodash contrib library can be installed using the following command:

npm install lodash-contrib –save

Example 1: 




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var array = [1, 2, -1, -1, 5, 6, -6, -6, -7, -8, 9, 9, 10];
  
// Creating array
var arr = _.third(array, 5);
  
console.log("Original Array : ", array);
console.log("Generated Array: ", arr);


Output:

Original Array :  [
   1,  2, -1, -1, 5, 6,
  -6, -6, -7, -8, 9, 9,
  10
]
Generated Array:  [ -1, -1, 5 ]

Example 2: If no index is passed, this method returns the third element from the original array.




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var array = [1, 2, -1, -1, 5, 6, -6, -6, -7, -8, 9, 9, 10];
  
// Creating array
var arr = _.third(array);
  
console.log("Original Array : ", array);
console.log("Element: ", arr);


Output: 

Original Array :  [
   1,  2, -1, -1, 5, 6,
  -6, -6, -7, -8, 9, 9,
  10
]
Element:  -1

Example 3: If the index passed is negative, the array is created up to the element on that index from right. 




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var array = [1, 2, -1, -1, 5, 6, -6, -6, -7, -8, 9, 9, 10];
  
// Creating array
var arr = _.third(array, -2);
  
console.log("Original Array : ", array);
console.log("Generated Array: ", arr);


Output: 

Original Array :  [
   1,  2, -1, -1, 5, 6,
  -6, -6, -7, -8, 9, 9,
  10
]
Generated Array:  [
  -1, -1,  5, 6, -6,
  -6, -7, -8, 9
]

Example 4: If the index is out of bounds, the remaining full array is created after the third element.




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var array = [1, 2, -1, -1, 5, 6, -6, -6, -7, -8, 9, 9, 10];
  
// Creating array
var arr = _.third(array, 100);
  
console.log("Original Array : ", array);
console.log("Generated Array: ", arr);


Output: 

Original Array :  [
   1,  2, -1, -1, 5, 6,
  -6, -6, -7, -8, 9, 9,
  10
]
Generated Array:  [
  -1, -1,  5, 6, -6,
  -6, -7, -8, 9,  9,
  10
]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS