📜  Lodash _.times() 方法

📅  最后修改于: 2022-05-13 01:56:31.977000             🧑  作者: Mango

Lodash _.times() 方法

Lodash _.times() 方法调用迭代 n 次,返回每个调用结果的数组。使用一个参数调用迭代对象。

句法:

_.times( n, iteratee )

参数:此方法接受上面提到的两个参数,如下所述:

  • n:调用iteratee函数的次数。
  • iteratee:每次迭代调用的函数。

返回值:它返回结果数组。

示例 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);


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);


输出:

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

示例 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);

输出:

[5, 5, 5]