Lodash _.cycle() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.cycle() method is then used to build a new array containing the given number of iterations of the given array, that are string end-to-end. Thus, the new array created contains the given array number of given times.
Syntax:
_.cycle( integer, array )
Parameters: This method takes two parameters as shown above and discussed below:
- integer: It is a number that specifies the number of times the given array is iterated.
- array: It is an array that is iterated through to make the new array.
Return Value: This method returns a new cycled array.
Note: This will not work in normal JavaScript because it requires the lodash-contrib library to be installed. The lodash-contrib library can be installed using npm install lodash-contrib –save
Example:
Javascript
// Defining lodash contrib variablevar _ = require('lodash-contrib'); Â
// Integer denoting times to// cycle through the arrayvar int = 12;Â
// Array that has to be cycledvar arr = [1, 2];Â
// Constructing cycled arrayvar c_arr = _.cycle(int, arr);Â
console.log("Cycled array : ");console.log(c_arr); |
Output:
Cycled array : [ 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



