Javascript 如果没有给出初始值,reduce方法将累加器设置为数组的第一项
尽管reduce函数的初始值为,但如果不提供它,reducer将使用输入数组的第一项初始化累加器。为什么Javascript 如果没有给出初始值,reduce方法将累加器设置为数组的第一项,javascript,Javascript,尽管reduce函数的初始值为,但如果不提供它,reducer将使用输入数组的第一项初始化累加器。为什么 const output = Object.keys({ name: "david", age: 30 }).reduce((memo, key) => { return [...memo, key]; }) console.log(output); // output: ["n", "a", "m", "e", "age"] vs 具体的代码示例只是为了说明这个问题
const output = Object.keys({
name: "david",
age: 30
}).reduce((memo, key) => {
return [...memo, key];
})
console.log(output);
// output: ["n", "a", "m", "e", "age"]
vs
具体的代码示例只是为了说明这个问题,我知道在不使用reduce的情况下使用Object.keys将返回相同的结果。使用Chrome 68进行测试。扩展运算符…memo将第一个值转换为字符数组。从
注意:如果未提供initialValue,reduce将执行
从索引1开始的回调函数,跳过第一个索引。如果
如果提供initialValue,它将从索引0开始
因此,如果没有初始值,您可以这样做。并不是说我认为它更好,而是它展示了被引用的行为
常量输出=Object.keys{
姓名:大卫,
年龄:30,,
鞋码:45
}.reduceemo,key,i=>i<2?[备忘录]:memo.concatkey;
//^因此将第一个值转换为数组
console.logoutput reduce可以递归定义,如:
// <Except some details to do with empty slots>
Array.prototype.reduce = function(f, initial_value) {
if (this.length === 0) {
if (arguments.length > 1) {
return initial_value;
}
throw TypeError('reduce of empty array with no initial value');
}
if (arguments.length > 1) {
// Starting value given.
const copy = this.slice();
const first = copy.shift();
return copy.reduce(f, f(initial_value, first));
} else {
// No starting value given
// What should happen here?
const copy = this.slice();
const first = copy.shift();
// return copy.reduce(f, f(intial_value, first));
// Nothing was given as the initial_value.
// What is the same as "nothing"?
// Applying the function with an `identity` value
// so that `f(identity, first) === first` means
// that this `identity` is the same as nothing.
// Of course, we can't know what that is if there
// are no elements in the list, so we threw an error before.
return copy.reduce(f, first);
}
};
一般来说,reduce的第二个参数应该是identity元素。对于像a,b=>a+b这样的函数,这将是0。对于a,b=>a*b,它应该是1。对于a,b=>Math.maxa,b,它应该是-无穷大
对于您的函数,正如您正确编写的那样,[]
通常,初始值应该是该标识。因此,如果您没有给出默认值,reduce将继续运行,就像您给了它函数的标识一样
在您的例子中,它稍微复杂一点,因为只有在定义相等以便输入x==输出[x]时,函数才具有标识
有关更多信息,请参阅。您认为它将使用什么来代替第一项?可能未定义?为什么?-因为这就是reduce的工作原理!从您链接的MDN页面:如果没有提供初始值,将使用数组中的第一个元素。在没有初始值的空数组上调用reduce是错误的。不知道你在问什么。好吧,这看起来是个不合逻辑的选择。我的意思是,如果您使用flowType或typescript,这显然意味着第一个参数(应该是累加器)可以是数组的一个项。我想知道这一选择的背后是什么。我知道有点哲学。我想我会记住在initialValue中添加null。
// <Except some details to do with empty slots>
Array.prototype.reduce = function(f, initial_value) {
if (this.length === 0) {
if (arguments.length > 1) {
return initial_value;
}
throw TypeError('reduce of empty array with no initial value');
}
if (arguments.length > 1) {
// Starting value given.
const copy = this.slice();
const first = copy.shift();
return copy.reduce(f, f(initial_value, first));
} else {
// No starting value given
// What should happen here?
const copy = this.slice();
const first = copy.shift();
// return copy.reduce(f, f(intial_value, first));
// Nothing was given as the initial_value.
// What is the same as "nothing"?
// Applying the function with an `identity` value
// so that `f(identity, first) === first` means
// that this `identity` is the same as nothing.
// Of course, we can't know what that is if there
// are no elements in the list, so we threw an error before.
return copy.reduce(f, first);
}
};