javascript 为什么 Array.prototype.reduce() 不将空数组作为累加器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32642506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:33:13  来源:igfitidea点击:

Why Array.prototype.reduce() is not taking an empty array as accumulator?

javascriptarraysreduce

提问by segmentationfaulter

I am trying to filter all the elements in an array which are bigger than 10 to a new array. I am intentionally not using Array.prototype.filter()since I want to learn the reduce()method. Here is the code I was playing with

我正在尝试将数组中大于 10 的所有元素过滤到新数组中。我故意不使用,Array.prototype.filter()因为我想学习该reduce()方法。这是我正在玩的代码

var collection = [3, 5, 11, 23, 1];

// fileter all the elements bigger than 10 to a new array

var output = collection.reduce(function(filteredArr, collectionElemet) {
  if (collectionElemet > 10) {
    return filteredArr.push(collectionElemet);
  }
}, []);

I was expecting that filteredArrwould be initialized with an empty array at the time of first callback execution as it happens with many examples provided here. But when I run this code, I get the error Cannot read property 'push' of undefined, where am I messing it up? Thank you!

我期待的是filteredArr将与第一个回调的执行时的空数组,因为它与提供的许多例子恰好被初始化这里。但是当我运行这段代码时,我得到了错误 Cannot read property 'push' of undefined,我在哪里搞砸了?谢谢!

回答by Paul

When you try to do return filteredArr.push(collectionElement), in essence you are returning length of filteredArr after the push operation. The push() method adds one or more elements to the end of an array and returns the new length of the array. Ref: Array.prototype.push().

当您尝试这样做时return filteredArr.push(collectionElement),本质上您是在推送操作后返回filteredArr 的长度。push() 方法将一个或多个元素添加到数组的末尾并返回数组的新长度。参考:Array.prototype.push()

You need to return the filteredArrfrom your anonymous function, so that it is used as the previousValuefor the next call

您需要filteredArr从匿名函数中返回,以便将其用作下一次调用的previousValue

var collection = [3, 5, 11, 23, 1];

// filter all the elements bigger than 10 to a new array

var output = collection.reduce(function(filteredArr, collectionElement) {
  if (collectionElement > 10) {
    filteredArr.push(collectionElement);
  }
  return filteredArr;
}, []);

回答by Josh Beam

Array.prototype.pushwill return the length of the new array. You need to return the accumulator. A succinct way to do this is with Array.prototype.concat, since that method will actually return the array:

Array.prototype.push将返回新数组的长度。您需要返回累加器。一种简洁的方法是使用Array.prototype.concat,因为该方法实际上会返回数组:

var collection = [3, 5, 11, 23, 1];

var output = collection.reduce(function(filteredArr, collectionElemet) {
  if (collectionElemet > 10) {
    return filteredArr.concat(collectionElemet);
  }
}, []);

You have to return the accumulator so the next iteration can use the value of the accumulator.

您必须返回累加器,以便下一次迭代可以使用累加器的值。