为什么我不能 return 在 .reduce() 函数中推送到累加器的结果?
Why can't I return the result of pushing to the accumulator in a .reduce() function?
简单的例子,想return一个数组的数组。对于 'peeps' 中的每个名称,我想将一个包含单词 'hello' 的数组推入累加器。
const peeps = ['sally', 'nick', 'dave'];
return peeps.reduce((acc, val) => {
return acc.push(['hello'])
}, []);
一直说acc.push()不是函数
谁能帮我理解为什么这不起作用。
您使用Array#push
The push()
method adds one or more elements to the end of an array and returns the new length of the array.
这个returns推入数组后的长度。然后累加器值是一个数字,而不是数组
return acc.push(['hello']) // 1
解决方案 1:Return 数组而不是推送的结果。
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.reduce((acc, val) => {
acc.push(['hello']);
return acc;
}, []));
解决方案 2:使用 Array#concat
.
The concat()
method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
虽然我会避免将它用于大量数据,因为它的效率比推送低很多。 jsPerf
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.reduce((acc, val) => acc.concat([['hello']]), []));
解决方案 3:使用 Array#map
。如果结果数组的长度应与给定数组的长度相同,则此方法效果最佳。
The map()
method creates a new array with the results of calling a provided function on every element in this array.
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.map(val => ['hello']));
试试这个:
const peeps = ['sally', 'nick', 'dave'];
return peeps.reduce((acc, val) => {
acc.push(['hello']);
return acc;
}, []);
push
没有 return acc,你必须手动完成。
我认为你应该试试地图。而不是减少。
const peeps = ['sally', 'nick', 'dave'];
return peeps.map((val) => {
return ['hello']
});
它将return一个数组[ ['Hello'],['Hello'],['Hello'] ]
Reduce 用于从数组中获取单个值。例如计数、总和、乘积等。
简单的例子,想return一个数组的数组。对于 'peeps' 中的每个名称,我想将一个包含单词 'hello' 的数组推入累加器。
const peeps = ['sally', 'nick', 'dave'];
return peeps.reduce((acc, val) => {
return acc.push(['hello'])
}, []);
一直说acc.push()不是函数
谁能帮我理解为什么这不起作用。
您使用Array#push
The
push()
method adds one or more elements to the end of an array and returns the new length of the array.
这个returns推入数组后的长度。然后累加器值是一个数字,而不是数组
return acc.push(['hello']) // 1
解决方案 1:Return 数组而不是推送的结果。
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.reduce((acc, val) => {
acc.push(['hello']);
return acc;
}, []));
解决方案 2:使用 Array#concat
.
The
concat()
method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
虽然我会避免将它用于大量数据,因为它的效率比推送低很多。 jsPerf
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.reduce((acc, val) => acc.concat([['hello']]), []));
解决方案 3:使用 Array#map
。如果结果数组的长度应与给定数组的长度相同,则此方法效果最佳。
The
map()
method creates a new array with the results of calling a provided function on every element in this array.
const peeps = ['sally', 'nick', 'dave'];
console.log(peeps.map(val => ['hello']));
试试这个:
const peeps = ['sally', 'nick', 'dave'];
return peeps.reduce((acc, val) => {
acc.push(['hello']);
return acc;
}, []);
push
没有 return acc,你必须手动完成。
我认为你应该试试地图。而不是减少。
const peeps = ['sally', 'nick', 'dave'];
return peeps.map((val) => {
return ['hello']
});
它将return一个数组[ ['Hello'],['Hello'],['Hello'] ]
Reduce 用于从数组中获取单个值。例如计数、总和、乘积等。