JavaScript reduce无法处理数学函数?

时间:2021-11-30 02:31:11

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):

我只能在这个形状下在node.js中运行这个例子(Array在node.js v4.12中没有连接,我现在使用它):

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?

为什么是这样?

3 个解决方案

#1


17  

The function passed to reduce takes more than 2 arguments:

传递给reduce的函数需要超过2个参数:

  • previousValue
  • PREVIOUSVALUE
  • currentValue
  • 当前值
  • index
  • 指数
  • array
  • 排列

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

Math.max将评估所有参数并返回最高:

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):

因此,在将Math.max传递给reduce的情况下,它将从传递的4个参数中返回最高值,其中一个是数组。传递数组会使Math.max返回NaN,因为数组不是数字。这是规格(15.8.2.11):

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

给定零个或多个参数,在每个参数上调用ToNumber并返回最大的结果值。

...

...

If any value is NaN, the result is NaN

如果任何值是NaN,则结果为NaN

ToNumber will return NaN for an array.

ToNumber将返回NaN作为数组。

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

因此,使用Math.max减少最终将返回NaN。

#2


4  

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

由于Math.max接受多个参数,因此您只需通过apply函数将数组转换为参数列表即可。

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

See warnings here on limitations of this technique.

请参阅此处有关此技术限制的警告。

#3


2  

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)

(说明如何减少工作,而不是从数组中获取最大数量的最佳方法)

#1


17  

The function passed to reduce takes more than 2 arguments:

传递给reduce的函数需要超过2个参数:

  • previousValue
  • PREVIOUSVALUE
  • currentValue
  • 当前值
  • index
  • 指数
  • array
  • 排列

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

Math.max将评估所有参数并返回最高:

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):

因此,在将Math.max传递给reduce的情况下,它将从传递的4个参数中返回最高值,其中一个是数组。传递数组会使Math.max返回NaN,因为数组不是数字。这是规格(15.8.2.11):

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

给定零个或多个参数,在每个参数上调用ToNumber并返回最大的结果值。

...

...

If any value is NaN, the result is NaN

如果任何值是NaN,则结果为NaN

ToNumber will return NaN for an array.

ToNumber将返回NaN作为数组。

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

因此,使用Math.max减少最终将返回NaN。

#2


4  

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

由于Math.max接受多个参数,因此您只需通过apply函数将数组转换为参数列表即可。

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

See warnings here on limitations of this technique.

请参阅此处有关此技术限制的警告。

#3


2  

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)

(说明如何减少工作,而不是从数组中获取最大数量的最佳方法)