混淆 Reduce 和 Arrow 函数
Confusing on Reduce and Arrow function
我正在看下面的post
。我可以理解其他部分,但对以下代码特别困惑。
const sum = integerSet.reduce((total, item) => total + Math.pow(item, pow), 0);
。
那么,在末尾添加 0 的目的是什么,如果我们从箭头函数更改为普通函数,它会更改为这个吗?
sum = integerSet.reduce(function(total, item) { return total + Math.pow(item,power),0; })
我已经搜索了几种reduce方法的用法,但仍然无法清楚地知道他是如何使用的,所以我决定在这里问一下。
0 为您提供起点。从那里开始,一切都将在这个累加器的“顶部”进行计算。因此,在您的示例中,0 将是 item
开头。
我正在看下面的post
const sum = integerSet.reduce((total, item) => total + Math.pow(item, pow), 0);
。
那么,在末尾添加 0 的目的是什么,如果我们从箭头函数更改为普通函数,它会更改为这个吗? sum = integerSet.reduce(function(total, item) { return total + Math.pow(item,power),0; })
我已经搜索了几种reduce方法的用法,但仍然无法清楚地知道他是如何使用的,所以我决定在这里问一下。
0 为您提供起点。从那里开始,一切都将在这个累加器的“顶部”进行计算。因此,在您的示例中,0 将是 item
开头。