Skip to content
Related Articles

Related Articles

Improve Article

Underscore.js _.iterators.slice() Method

  • Last Updated : 01 Sep, 2020

With the help of _.iterators.slice() method, we can get the value from the iteration function but it will drop the number of values which is given by numberToDrop and return the maximum of numberToTake of the remaining values one by one whenever invoked by using this function.

Syntax:

_.iterators.slice(iter, numberToDrop, numberToTake)

Paramter: This method accepts three parameter as mentioned above and described below:

  • iter: This parameter holds the iterator list of the array.
  • numberToDrop: This parameter the value to drop.
  • numberToTake: This parameter the value to take.

Return value: Return the values whenever iteration function invoked.

Note: To execute the below examples you have to install the underscore-contrib library by using this command prompt we have to execute the following command.



npm install underscore-contrib

Below examples illustrate the Underscore.js _.iterators.slice() method i JavaScript:

Example 1: In this example we can see that by using _.iterators.slice() method, we are able to get the values after dropping some values starts from numberToDrop and return values maximum number of values defined as numberToTake by using this method.

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List([1, 2, 3, 4, 5, 6, 7, 8]);
  
var geek = _.iterators.slice(iter, 4, 2);
  
for(var i = 0; i < 3; i++) {
    console.log(geek());
}

Output :

5
6

Example 2:

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List(["ABC", "XYZ", "Geeks",
                             "for", "Geeks"]);
var geek = _.iterators.slice(iter, 2, 3);
  
for(var i = 0; i < 3; i++) {
    console.log(geek());
}

Output :

Geeks
for
Geeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :