了解JavaScript中的.reduce()函数

时间:2018-04-03 01:02:28

标签: javascript ecmascript-6

有人可以解释一下这段代码吗?

我得到了.concat和.map函数但是我混淆了为什么有两个.reduce方法来打印出不同向量的所有可能的排列。

var arr = [['red','blue','pink'],['dog','cat','bird'],['loud', 'quiet']],
    res = arr.reduce((p,c) => p.reduce((r,x) => r.concat(c.map(y => x + " " + y)),[]));
console.log(res);

两种.reduce方法有什么作用?

2 个答案:

答案 0 :(得分:1)

Lamdas让你不要说出事情

df$location <- gsub("_", " ", gsub("(^|_)([a-z])", "\\1\\U\\2", df$location, perl = TRUE))

这是我对它进行扩展,使用内部console.log,可以更容易地看到它正在进行中。

答案 1 :(得分:1)

通过遍历reducer中的每个项目来减少尝试将数组转换为1个单项(reducer是一个函数)。

在你的例子中

arr将被 reducer 减少 (p,c)=&gt; p.reduce((r,x)=&gt; r.concat(c.map(y =&gt; x +“”+ y)),[])

此还原剂包含另一个含有还原剂的还原剂 (r,x)=&gt; r.concat(c.map(y =&gt; x +“”+ y)), []

这个reducer对传入的项目做了2个不同的事情,这些项目是concat和map,它还有一个初始值 [] (意味着它开始从空数组遍历项目到reducer) concat是合并一个数组 map是为主题中的每个项执行定义的操作

因此,可以如下详述完整操作。执行此代码还可以查看语句如何在2 reducer上顺序运行

var arr = [['red','blue','pink'],['dog','cat','bird'],['loud', 'quiet']],
    res = arr.reduce(
            (p,c) => {
                console.log("first reducer",p,c);
            return p.reduce(
                (r,x) => {
                        console.log("second reducer",r,x);
                        return r.concat(c.map(y => {
                                        console.log("map",x,y); 
                                        return x + " " + y;}));
                        }
                ,[])
            }
        );
console.log("ans",res);

还原过程的结果

VM928:4 first reducer ["red", "blue", "pink"] ["dog", "cat", "bird"]
VM928:7 second reducer [] red
VM928:9 map red dog
VM928:9 map red cat
VM928:9 map red bird
VM928:7 second reducer ["red dog", "red cat", "red bird"] blue
VM928:9 map blue dog
VM928:9 map blue cat
VM928:9 map blue bird
VM928:7 second reducer ["red dog", "red cat", "red bird", "blue dog", "blue cat", "blue bird"] pink
VM928:9 map pink dog
VM928:9 map pink cat
VM928:9 map pink bird
VM928:4 first reducer ["red dog", "red cat", "red bird", "blue dog", "blue cat", "blue bird", "pink dog", "pink cat", "pink bird"] ["loud", "quiet"]
VM928:7 second reducer [] red dog
VM928:9 map red dog loud
VM928:9 map red dog quiet
VM928:7 second reducer ["red dog loud", "red dog quiet"] red cat
VM928:9 map red cat loud
VM928:9 map red cat quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet"] red bird
VM928:9 map red bird loud
VM928:9 map red bird quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet"] blue dog
VM928:9 map blue dog loud
VM928:9 map blue dog quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet"] blue cat
VM928:9 map blue cat loud
VM928:9 map blue cat quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet", "blue cat loud", "blue cat quiet"] blue bird
VM928:9 map blue bird loud
VM928:9 map blue bird quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet", "blue cat loud", "blue cat quiet", "blue bird loud", "blue bird quiet"] pink dog
VM928:9 map pink dog loud
VM928:9 map pink dog quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet", "blue cat loud", "blue cat quiet", "blue bird loud", "blue bird quiet", "pink dog loud", "pink dog quiet"] pink cat
VM928:9 map pink cat loud
VM928:9 map pink cat quiet
VM928:7 second reducer ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet", "blue cat loud", "blue cat quiet", "blue bird loud", "blue bird quiet", "pink dog loud", "pink dog quiet", "pink cat loud", "pink cat quiet"] pink bird
VM928:9 map pink bird loud
VM928:9 map pink bird quiet
VM928:15 ans ["red dog loud", "red dog quiet", "red cat loud", "red cat quiet", "red bird loud", "red bird quiet", "blue dog loud", "blue dog quiet", "blue cat loud", "blue cat quiet", "blue bird loud", "blue bird quiet", "pink dog loud", "pink dog quiet", "pink cat loud", "pink cat quiet", "pink bird loud", "pink bird quiet"]

因此,还原过程arr.reduce((p,c)=&gt; p(c))通过reducer p((r,x)=&gt; r(x))将数组的数组减少为1个数组,

reducer p((r,x)=&gt; r(x))尝试连续并从最内部的项目逐个映射,但是, map 正在使用 x 在公式中(x是1X3数组),它导致数组的大小没有减少(因为映射数组(1x3)而数组(1x1)意味着返回一个数组(1x3))之后减少过程已经完成。