Wednesday, July 3, 2024
HomeLanguagesJavascriptUnderscore.js _.iterators.take() method

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 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 variable
const _ = 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 variable
const _ = 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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments