function splitArray(array){
let realArray = [array].reduce(a,b => {
a.concat(b);
});
}
console.log(splitArray([[1, 2, 3], [4, 5], [6]]));
最佳答案
你需要
- 返回缩减后的结果,
- 包裹 arrow function 的参数(如果有多个) ,
- 返回 concat 的值。
function splitArray(array) {
return array.reduce((a, b) => {
return a.concat(b);
});
}
console.log(splitArray([[1, 2, 3], [4, 5], [6]]));
通过仅使用一个表达式返回,您可以省略大括号和 return 语句。
function splitArray(array) {
return array.reduce((a, b) => a.concat(b));
}
console.log(splitArray([[1, 2, 3], [4, 5], [6]]));
关于javascript - 展平数组,即 [[1, 2, 3], [4, 5], [6]] 应该给出 [1, 2, 3, 4, 5, 6] 的输出。代码中的错误来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51980788/