Reputation: 4588
I assume the answer to this question is no, but I'm not sure. Below is a reduce function , one is not using an arrow functions and one is. Is it possible to incorporate the second argument of the first into the arrow style ?
var s = arr.reduce(function(){
},0) // includes second argument
and........
var a = arr.reduce = () => {
} // ?
Upvotes: 3
Views: 106
Reputation: 87203
Yes, Arrow functions can work with multiple params, they just need to be added inside parenthesis.
var s = arr.reduce((accum, currVal) => accum + currVal, 0);
^ ^ : Multiple arguments to the Arrow function
^^^: Second argument of the `reduce` function
Here, the second parameter to the Array#reduce
can be passed normally. The arrow function(first parameter) has no effect on how the second argument is passed.
Upvotes: 6
Reputation: 1075129
The part of this code:
var s = arr.reduce(function(){
},0) // includes second argument
...that an arrow function would replace is purely this bit:
function() {
}
E.g.:
var s = arr.reduce(/*The function
goes
here*/,0) // includes second argument
The 0
is not related to the function being passed, it's a second argument to reduce
.
So the equivalent of your first block is:
var s = arr.reduce(() => {
}, 0) // includes second argument
Although of course, if you're using reduce
, in both code blocks you're going to want some arguments:
var s = arr.reduce(function(result, current) {
return /*...something combining `result` with `current`...*/;
}, 0);
So:
var s = arr.reduce((result, current) => {
return /*...something combining `result` with `current`...*/;
}, 0);
Or:
var s = arr.reduce((result, current) => /*...something combining `result` with `current`...*/, 0)
Upvotes: 4
Reputation: 665040
You have to call the host function as usual, and can supply multiple arguments as usual. Just replace the function expression by the arrow function.
var s = arr.reduce(() => {
…
}, 0);
Your second snippet did overwrite (assign to) arr.reduce
, not invoke it.
Upvotes: 2