我已经阅读了how to read an array in MDN in JS中的代码。工作得很好,但我不明白为什么在这种情况下不工作:
const data = [null, ['a', 'b', 'c']]
const flattened = data.reduce((acc, cur) => {
if(null != cur)
acc.concat(cur)
}, [])还有这个错误:
TypeError: Cannot read property 'concat' of undefined如何纠正这一点?
发布于 2017-05-14 08:18:37
传递给.reduce()的函数不返回值
const data = [null, ['a', 'b', 'c']]
const flattened = data.reduce((acc, cur) => {
if (cur !== null)
acc = acc.concat(cur);
return acc
}, []);
console.log(flattened);
https://stackoverflow.com/questions/43959195
复制相似问题