Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.cycle() Method

  • Last Updated : 14 Sep, 2020

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 strung end-to-end. Thus, the new array created contains the given array number of given times.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


_.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



// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
// Integer denoting times to
// cycle through the array
var int = 12;
// Array that has to be cycled
var arr = [1, 2];
// Constructing cycled array
var c_arr = _.cycle(int, arr);
console.log("Cycled array : ");


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
My Personal Notes arrow_drop_up
Recommended Articles
Page :