Javascript 使用reduce函数返回一个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35431292/
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
Using the reduce function to return an array
提问by 2K01B5
Why is it that when I want to use the push function inside the reduce function to return a new array I get an error. However, when I use the concat method inside the reduce function, it returns a new array with no problem.
为什么当我想在reduce 函数中使用push 函数返回一个新数组时出现错误。但是,当我在 reduce 函数中使用 concat 方法时,它返回一个没有问题的新数组。
All I'm trying to do is pass an array to the reduce function and return the same array.
我要做的就是将数组传递给reduce 函数并返回相同的数组。
var store = [0,1,2,3,4];
var stored = store.reduce(function(pV,cV,cI){
console.log("pv: ", pV);
return pV.push(cV);
},[]);
This returns an error. But when I use concat:
这将返回一个错误。但是当我使用 concat 时:
var store = [0,1,2,3,4];
var stored = store.reduce(function(pV,cV,cI){
console.log("pv: ", pV);
return pV.concat(cV);
},[]);
It returns the same array.
它返回相同的数组。
Any ideas why?
任何想法为什么?
回答by Venkata Raju
push
returns the new length of the array.
push
返回数组的新长度。
What you need is the initially provided array.
您需要的是最初提供的数组。
So change the code as below.
所以更改代码如下。
var store = [0, 1, 2, 3, 4];
var stored = store.reduce(function(pV, cV, cI){
console.log("pv: ", pV);
pV.push(cV);
return pV; // ********* Important ******
}, []);
concat
returns the new array combining the elements of the provided array and concatenated elements. so it works.
concat
返回组合提供的数组元素和连接元素的新数组。所以它有效。
回答by Jamiec
Just for completeness, and for the next person who happens on this question, what you're doing is typically achieved with map
which, as stated in the docs
只是为了完整性,对于下一个遇到这个问题的人,你所做的通常是通过map
它来实现的,如文档中所述
map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results
map 为数组中的每个元素依次调用一次提供的回调函数,并根据结果构造一个新数组
Contrast that with the description of reduce
:
对比一下描述reduce
:
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
reduce() 方法对累加器和数组的每个值(从左到右)应用一个函数以将其减少为单个值。
(Emphasis mine) So you see, although you canmanipulate reduce
to return a new array, it's general usage is to reduce an array to a single value.
(强调我的)所以你看,虽然你可以操纵reduce
返回一个新数组,但它的一般用法是将数组减少到单个值。
So for your code this would be:
所以对于你的代码,这将是:
var store = [0,1,2,3,4];
var stored = store.map(function(pV){
console.log("pv: ", pV);
return pV;
});
Much simpler than trying to reconstruct a new array using either push
or concat
within a reduce
function.
比尝试使用函数push
或concat
在reduce
函数内重建新数组要简单得多。
回答by abitofcode
reduce can be useful if you need to return an array with multiple items for each item iterated;
如果您需要为迭代的每个项目返回一个包含多个项目的数组,reduce 会很有用;
var inputs = media.reduce((passedArray, video) => {
passedArray.push("-i");
passedArray.push(video.filepath);
return passedArray;
}, []);
var inputs = media.reduce((passedArray, video) => {
passedArray.push("-i");
passedArray.push(video.filepath);
return passedArray;
}, []);
Here it's being used to build the input array for ffmpeg;
这里它被用来为 ffmpeg 构建输入数组;
[{ name: "bob", filepath: "1.mp4" }, { name: "sue", filepath: "3.mp4" }]
[{ name: "bob", filepath: "1.mp4" }, { name: "sue", filepath: "3.mp4" }]
=> ["-i", "1.mp4", "-i", "2.mp4]
=> ["-i", "1.mp4", "-i", "2.mp4]
回答by dfsq
Array.prototype.pushmethod returns the new length of the array.
Array.prototype.push方法返回数组的新长度。
Array.prototype.concatmethod inserts new element into array and returns array back so it can be further processed. This is what you need to do with reduce: pass modified array the the next iteration.
Array.prototype.concat方法将新元素插入数组并返回数组,以便进一步处理。这就是你需要用 reduce 做的事情:在下一次迭代中传递修改过的数组。
回答by jean d'arme
You can alway use destructuring:
你总是可以使用解构:
var store = [0,1,2,3,4];
var stored = store.reduce(function(pV,cV,cI){
console.log("pv: ", pV);
return [...pV, cV];
},[]);
console.log(stored);