Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.cycle() Method

Underscore.js _.cycle() Method

The _.cycle() Method takes an integer value and an array which is then used to build an array containing the number of iterations through the given array, string end-to-end.

A new array created contains the given array number of given times.

Syntax:

_.cycle(integer, array);

Parameters:

  • integer: The number of times the given array is iterated.
  • array: The array which is iterated to make a new array

Return Value: This method returns a cycled array.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.

underscore.js contrib library can be installed using 

npm install underscore-contrib --save

Example: In this example, we will simply create a cycled array using this method.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Integer
let int = 10;
// Array
let arr = [1, 2, 3];
// Constructing cycled array
let c_arr = _.cycle(int, arr);
console.log("cycled array : ");
console.log(c_arr);


Output:

cycled array :
[
  1, 2, 3, 1, 2, 3, 1, 2, 3,
  1, 2, 3, 1, 2, 3, 1, 2, 3,
  1, 2, 3, 1, 2, 3, 1, 2, 3,
  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!

RELATED ARTICLES

Most Popular

Recent Comments