【发布时间】:2014-06-15 01:36:30
【问题描述】:
当我减少数组时,我试图让数字为零,但我不清楚函数的行为
[].reduce(function(previousValue, currentValue){
return Number(previousValue) + Number(currentValue);
});
结果
TypeError: Reduce of empty array with no initial value
似乎如果数组为空我无法减少它
[""].reduce(function(previousValue, currentValue){
return Number(previousValue) + Number(currentValue);
});
结果
""
如果数组中唯一的元素是一个空字符串,则检索一个空字符串
【问题讨论】:
标签: javascript arrays ecmascript-5