With the help of _.iterators.take() method, we can get the values from the iteration function starting from 1 to the numberToTake variable and return a value whenever the iteration function is invoked by using this method.
Syntax:
_.iterators.take(iter, numberToTake)
Return: Return the value from the iteration function.
Example 1: In this example, we can see that by using _.iterators.take() method, we are able to get the values from the iteration function up to the maximum value of numberToTake whenever we invoked the iteration function.
javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
let iter = _.iterators.List(["Geeks", "for", "Geeks", "ABC", "XYZ"]);Â
let geek = _.iterators.take(iter, 3);Â
for (let i = 0; i < 3; i++) {Â Â Â Â console.log(geek());} |
Output:
Geeks for Geeks
Example 2: In this example, we will see the use of the _.iterators.take() method
javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
let iter = _.iterators.List([1, 2, 3, 4, 5, 6]);Â
let geek = _.iterators.take(iter, 3);Â
for (let i = 0; i < 3; i++) {Â Â Â Â console.log(geek());} |
Output:
1 2 3
