[英]Why is there an array at the end of this function using reduce?
I am working on an exercise involving the reduce array method.我正在做一个涉及 reduce 数组方法的练习。 After the accumulator is returned what is the reason for adding the
, []
?返回累加器后,添加
, []
的原因是什么? Is it just an empty array?它只是一个空数组吗? My tests did not pass without it until after viewing the answer and adding it to my function. Thanks!
在查看答案并将其添加到我的 function 之前,我的测试没有通过它。谢谢!
function extractValue(arr, key) { return arr.reduce((accum, nextName) => { accum.push(nextName[key]); return accum; }, []); } // const arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}] // extractValue(arr,'name') // ['Elie', 'Tim', 'Matt', 'Colt']
Just read the official documentation for the.reduce method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce只需阅读 .reduce 方法的官方文档https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
That empty array is the initialValue parameter.该空数组是 initialValue 参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.