Javascript lodash';parseInt在地图上安全地解析

Javascript lodash';parseInt在地图上安全地解析,javascript,lodash,Javascript,Lodash,我在读一篇博客,上面写着u.parseInt是安全的。 根据文档,它还接受基数作为本机parseInt的第二个参数。 通常在映射数组时,直接将parseInt传递给map时可能会遇到意外行为 lodash的parseInt如何安全工作 var a = ['2', '3', '4', '5', '6', '7', '8'] //case 1: _.map(a, parseInt) //[2, NaN, NaN, NaN, NaN, NaN, NaN] - this is the exp

我在读一篇博客,上面写着u.parseInt是安全的。 根据文档,它还接受基数作为本机parseInt的第二个参数。 通常在映射数组时,直接将parseInt传递给map时可能会遇到意外行为

lodash的parseInt如何安全工作

var a = ['2', '3', '4', '5', '6', '7', '8']

//case 1:    
_.map(a, parseInt)
//[2, NaN, NaN, NaN, NaN, NaN, NaN] -  this is the expected output

//case 2:    
_.map(a, (num, index) => _.parseInt(num, index))
//[2, NaN, NaN, NaN, NaN, NaN, NaN] -  this is the expected output

//case 3:    
_.map(a, _.parseInt)
//[2, 3, 4, 5, 6, 7, 8] -  how is this working correctly?
还有,案例2与案例3有何不同?

接受一个“秘密”的第三个参数

如果提供了第三个参数,比如在
.map(a,..parseInt)
回调中,则忽略第二个参数

var a=['2','3','4','5','6','7','8'];
//有两个论点:
log(u.map(a,(num,index)=>u.parseInt(num,index));
//[2,NaN,NaN,NaN,NaN,NaN]-这是预期输出
//使用u.map提供的所有三个参数:
log(u.map(a,(num,index,arr)=>u.parseInt(num,index,arr));
//[2,3,4,5,6,7,8]

谢谢,我在发帖后的源代码中看到了这一点,但这是为了让它在map、reduce等方面发挥作用而专门做的吗?我无法想象还有其他原因。这也表明了这一点。