JavaScript reduce can't handle Math functions?

旧巷老猫 提交于 2019-12-12 10:29:39

问题


I'm trying an obvious task:

var maxVal = [ 1, 2, 3, 4, 5 ].reduce( Math.max, 0 );

and get:

NaN

as the result. To make it work I have to make an anonymous function this way:

var maxVal = [ 1, 2, 3, 4, 5 ].reduce( function ( a, b ) { 
                                           return Math.max(a, b);
                                       }, 0 );

Could someone tell me why? Both are functions that take two arguments and both return one value. What's the difference?

Another example could be this:

var newList = [[1, 2, 3], [4, 5, 6]].reduce( Array.concat, [] );

The result is:

[1, 2, 3, 0, #1=[1, 2, 3], #2=[4, 5, 6], 4, 5, 6, 1, #1#, #2#]

I can run this example in node.js only under this shape (Array has no concat in node.js v4.12, which I use now):

var newList = [[1, 2, 3], [4, 5, 6]].reduce( [].concat, [] );    

and then get this:

[ {}, {}, 1, 2, 3, 0, [ 1, 2, 3 ], [ 4, 5, 6 ], 4, 5, 6, 1, [ 1, 2, 3 ], [ 4, 5, 6 ] ]

And why is that?


回答1:


The function passed to reduce takes more than 2 arguments:

  • previousValue
  • currentValue
  • index
  • array

Math.max will evaluate all arguments and return the highest:

Math.max(1, 2, 3, 4) === 4;

So in case of passing Math.max to reduce, it will return the highest value from the 4 arguments passed, of which one is an array. Passing an array will make Math.max return NaN because an array is not a number. This is in the specs (15.8.2.11):

Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

...

If any value is NaN, the result is NaN

ToNumber will return NaN for an array.

So reducing with Math.max will return NaN in the end.




回答2:


Since Math.max accepts multiple arguments, you can just convert the array to a parameter list via the apply function.

var maxVal = Math.max.apply(Math, [ 1, 2, 3, 4, 5 ]);

See warnings here on limitations of this technique.




回答3:


Another way to reduce an array to its max value:

var maxVal = [ 1, 2, 3, 4, 5 ].reduce(function(a,b){return a>b?a:b});

(illustrating how reduce works, not that it is the best way to get the max number from an array)



来源:https://stackoverflow.com/questions/7767748/javascript-reduce-cant-handle-math-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!