Underscore.js _.iterators.slice() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
}

chevron_right


Output :

5
6

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
}

chevron_right


Output :

Geeks
for
Geeks

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.