【发布时间】:2022-01-16 08:25:46
【问题描述】:
这是返回一个数字数组,该数组是基本数组中数组的最大值。当我使用 for 语句时,我可以让它工作。但我试图简化它,但无法弄清楚为什么它不起作用。任何帮助都会得到帮助。
function largestOfFour(arr) {
return arr.map((x) => x.reduce((a, c) => c > a ? c : a, 0));
}
输入输出示例:
const input = [
[1,2,3,4,5],
[6,5,4,3,2,1],
[1,7,3,4,5,6],
];
function findHighestElements(arr) {
return arr.map((x) => x.reduce((a, c) => c > a ? c : a, 0));
}
console.log(findHighestElements(input)); // [5,6,7]
【问题讨论】:
-
请添加输入和预期输出。
-
什么不起作用?预期的输入和输出是什么?你会得到什么作为输出?
-
[[1, 2, 3][4, 5, 6][7, 8, 9]]->[[1, 2, 3], [4, 5, 6], [7, 8, 9]]你没有用逗号分隔数组
标签: javascript dictionary reduce