Lodash _.times() Method
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:
- 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: In this example, we are printing the 5 times String by the use of the lodash _times() method.
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: In this example, we are printing the 3 times constant by the use of the lodash _times() method.
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]
Contact Us