简体   繁体   English

使用reduce函数返回一个数组

[英]Using the reduce function to return an array

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.为什么当我想在reduce 函数中使用push 函数返回一个新数组时出现错误。 However, when I use the concat method inside the reduce function, it returns a new array with no problem.但是,当我在 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?任何想法为什么?

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. concat返回组合提供的数组元素和连接元素的新数组。 so it works.所以它有效。

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 can manipulate 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.比尝试在reduce函数中使用pushconcat重建新数组要简单得多。

I know this is the same answer, but I just want to show that using reduce () , the syntax can also be reduced to a single line of code using ES6:我知道这是相同的答案,但我只想表明使用reduce () ,语法也可以使用 ES6 简化为一行代码:

 var store = [0,1,2,3,4]; var stored = store.reduce((pV,cV) => [...pV, cV], []); console.log(stored);

Array.prototype.push method returns the new length of the array. Array.prototype.push方法返回数组的新长度。

Array.prototype.concat method inserts new element into array and returns array back so it can be further processed. Array.prototype.concat方法将新元素插入数组并返回数组,以便进一步处理。 This is what you need to do with reduce: pass modified array the the next iteration.这就是你需要用 reduce 做的事情:在下一次迭代中传递修改过的数组。

You can always 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);

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; }, []);

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]

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

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