Underscore.js _.third() Method

The _.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:

  • 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 underscore.js contrib library to be installed.



underscore.js contrib library can be installed using npm install underscore-contrib –save.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-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);

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-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);

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-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);

chevron_right


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
]

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.