Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.times() Method

  • Last Updated : 09 Sep, 2020

The Lodash _.times() method Invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with one argument.

Syntax: 

_.times( n, iteratee )

Parameters: This method accepts two parameters as mentioned above and described below:

  • n: The number of times to invoke iteratee function.
  • iteratee: The function which is invoked per iteration.

Return Value: It Returns the array of results.

Example 1:



Javascript




// Requiring the lodash library  
const _ = require("lodash");            
    
// Use of _.times() method 
// by taking n=5 and string
let gfg = _.times(5,String); 
        
// Printing the output  
console.log(gfg);

Output:

["0", "1", "2", "3", "4"]

Example 2:

Javascript




// Requiring the lodash library  
const _ = require("lodash");            
    
// Use of _.times() method 
// by taking n=3 and constant method
let gfg = _.times(3,_.constant(5)); 
        
// Printing the output  
console.log(gfg);

Output:

[5, 5, 5]
My Personal Notes arrow_drop_up
Recommended Articles
Page :