Sunday, September 7, 2025
HomeLanguagesJavascriptUnderscore.js _.takeSkipping() Method

Underscore.js _.takeSkipping() Method

The _.takeSkipping() method takes an array and a skip value (say n) and creates an array containing every nth element of the original array.

For an index >= 0 first element of the new array is always the same as the first element of the original array.

Syntax:

_.takeSkipping(array, skip_value)

Parameters: 

  • array: The array from which skip array is to be made.
  • skip_value: The value which is used to make skip array from original array.

Return Value: This method returns a newly created skip 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: In this example, we will generate a skip array using this method.

Javascript




// Defining underscore contrib variable
  
var _ = require('underscore-contrib'); 
// Array
  
var array = [1, 2, 3, 4, 6, 4, 3, 10];
  
// skipValue
var skip = 2;
  
// Generating Array using takeSkipping() method
var arr =_.takeSkipping(array, skip);
console.log("Array : ", array);
console.log("Skip Value : ", skip);
console.log("Generated Array : ", arr);


Output: generated skip array contains the (n+2)th indexed element.

Array :  [
  1, 2, 3, 4,
  6, 4, 3, 10
]
Skip Value :  2
Generated Array :  [ 1, 3, 6, 3 ]

Example 2: This method is safe for skip values greater or lesser than the array size.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Array
var array = [1, 2, 3, 4, 6, 4, 3, 10];
  
// skipValue
var skip = 20;
  
// Generating Array using takeSkipping() method
var arr =_.takeSkipping(array, skip);
console.log("Array : ", array);
console.log("Skip Value : ", skip);
console.log("Generated Array : ", arr);


Output: Only first element or 0th indexed element is in the generated array.

Array :  [
  1, 2, 3, 4,
  6, 4, 3, 10
]
Skip Value :  2
0
Generated Array :  [ 1 ]

Example 3: For negative skip value it returns an empty array.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Array
var array = [1, 2, 3, 4, 6, 4, 3, 10];
  
// skipValue
var skip = -20;
  
// Generating Array using takeSkipping() method
var arr =_.takeSkipping(array, skip);
console.log("Array : ", array);
console.log("Skip Value : ", skip);
console.log("Generated Array : ", arr);


Output:

Array :  [
  1, 2, 3, 4,
  6, 4, 3, 10
]
Skip Value :  -20
0
Generated Array :  []
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
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11870 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS