Underscore.js _.iterators.take() method

With the help of _.iterators.take() method, we can get the values from the iteration function starting from 1 to numberToTake variable and return a value whenever iteration function is invoked by using this method.

Syntax:

_.iterators.take(iter, numberToTake)

Return: Return the value from iteration function.

Example 1:

In this example we can see that by using _.iterators.take() method, we are able to get the values from iteration function upto maximum value of numberToTake whenever we invoked the iteration function.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Geeks
for
Geeks

Example 2:

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]);
  
var geek = _.iterators.take(iter, 3);
  
for(var i = 0; i < 3; i++) {
    console.log(geek());
}

chevron_right


Output:

1
2
3

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.