我想这是两个问题.我仍然遇到使用reduce方法的问题,我得到了使用它的简单方法
reduce([1,2,3], function(a, b) {
return a + b;
}, 0);
//6
使用除数字以外的任何东西真的让我很困惑.那么我将如何使用reduce代替for循环来构建contains函数?评论将不胜感激.谢谢你们.
function contains(collection, target) {
for(var i=0; i < collection.length; i++){
if(collection[i] === target){
return true;
}
}
return false;
}
contains([1, 2, 3, 4, 5], 4);
//true
Run Code Online (Sandbox Code Playgroud)