Open In App

Lodash _.takeSkipping() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

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

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

Return Value:

This method returns a newly created skip 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: In this example, the code uses the Lodash Contrib library to extract every nth element from an array and store it in a new array using the _.takeSkipping() method.

In this example, we will generate a skip array using this method.

javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Array
let array = [1, 2, 3, 4, 6, 4, 3, 10];
 
// SkipValue
let skip = 2;
 
// Generating Array using takeSkipping() method
let 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: In this example, the code uses the Lodash Contrib library to extract every nth element from an array and store it in a new array using the _.takeSkipping() method, even if the skip value is larger than the array length. This method is safe for skip values greater or lesser than the array size.

javascript




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


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

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

Example 3: In this example, the code uses the Lodash Contrib library to extract every nth element from an array and store it in a new array using the _.takeSkipping() method, even if the skip value is negative. For negative skip value it returns an empty array.

javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Array
let array = [1, 2, 3, 4, 6, 4, 3, 10];
 
// skipValue
let skip = -10;
 
// Generating Array using takeSkipping() method
let 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 : -10
Generated Array : []


Last Updated : 09 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads