简体   繁体   English

Javascript 减少一个空数组

[英]Javascript Reduce an empty array

When I reduce the array, I am trying to get the number zero, but I dont clearly understand the behaviour of the function当我减少数组时,我试图得到数字零,但我不清楚函数的行为

[].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
});

result结果

TypeError: Reduce of empty array with no initial value

seems that if the array is empty I can't reduce it似乎如果数组为空我不能减少它

[""].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
});

result结果

""

If the only element in the array is an empty string, retrieves an empty string如果数组中唯一的元素是空字符串,则检索空字符串

The second parameter is for initial value. 第二个参数是初始值。

[].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
}, 0);

or using ES6:或使用 ES6:

[].reduce( (previousValue, currentValue) => previousValue + currentValue, 0);

Both behaviors are according to the spec .这两种行为都符合规范

You cannot reduce an empty array unless you explicitly provide an initial "accumulated" value as the second argument:除非您明确提供初始“累积”值作为第二个参数,否则您不能reduce空数组:

If no initialValue was provided, then previousValue will be equal to the first value in the array and currentValue will be equal to the second.如果未提供 initialValue,则 previousValue 将等于数组中的第一个值,而 currentValue 将等于第二个值。 It is a TypeError if the array contains no elements and initialValue is not provided.如果数组不包含元素且未提供 initialValue,则为 TypeError。

If the array has at least one element then providing an initial value is optional.如果数组至少有一个元素,则提供初始值是可选的。 However, if one is not provided then the first element of the array is used as the initial value and reduce continues to process the rest of the array elements by invoking your callback.但是,如果未提供,则使用数组的第一个元素作为初始值,并通过调用回调来reduce数组的其余元素。 In your case the array only contains a single element, so that element becomes the initial value and also final value, since there are no more elements to be processed through the callback.在您的情况下,数组仅包含一个元素,因此该元素成为初始值和最终值,因为没有更多元素要通过回调处理。

in your example, the previous value must have to defined first, because it is equivalent to assigning a value to a variable previousvale在您的示例中,必须先定义前一个值,因为它相当于为变量 previousvale 赋值

let previousvalue = 0;
is different from
let previousvalue;

Just ran into this myself and want to give this an answer with a more simple yet modern JavaScript example.我自己刚遇到这个问题,想用一个更简单但现代的 JavaScript 示例给出答案。

JavaScript's array reduce function iterates over an array (here list ) and reduces it to a new result (here sum ) by computing each item (here value ) in the array with the accumulator (here acc ): JavaScript 的数组 reduce 函数遍历一个数组(这里是list ),并通过使用累加器(这里是acc )计算数组中的每个项目(这里是value )来将其缩减为一个新结果(这里是sum ):

const sum = list.reduce((acc, value) => acc + value);

Most reduce operations prefer to operate on an initial accumulator (second argument, also called initial value).大多数reduce 操作更喜欢对初始累加器(第二个参数,也称为初始值)进行操作。 In our example, it's indeed even mandatory, because otherwise the sum operation wouldn't work.在我们的示例中,它甚至是强制性的,因为否则求和运算将不起作用。 We want to sum our values with an initial value of 0:我们想将我们的值与初始值为 0 相加:

const sum = list.reduce((acc, value) => acc + value, 0);

The initial value for a reduce function is its second argument (here 0 ). reduce 函数的初始值是它的第二个参数(这里是0 )。 So if you come across the error:因此,如果您遇到错误:

TypeError: Reduce of empty array with no initial value

It just means that you have to provide an initial (empty) array for your reduce operation, because if you provide an empty array (here list ) to start with, it doesn't work and complains with this error.这只是意味着你必须为你的reduce 操作提供一个初始(空)数组,因为如果你提供一个空数组(这里是list )开始,它不起作用并抱怨这个错误。 In order to fix it, provide an empty array as second argument:为了修复它,提供一个空数组作为第二个参数:

const exponentials = list.reduce((acc, value) => {
  acc = acc.concat(value * value);
  return acc;
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM