使用 map JavaScript 返回每个数组中的最大数字
javascriptweb developmentobject oriented programming更新于 2025/10/13 3:24:17
我们有一个数字数组,例如这个 −
const arr = [ [12, 56, 34, 88], [65, 66, 32, 98], [43, 87, 65, 43], [32, 98, 76, 83], [65, 89, 32, 54], ];
我们需要编写一个函数来映射这个数组,并返回一个包含每个子数组中最大(最大)元素的数组。
因此,让我们编写该函数的代码 −
示例
const arr = [
[12, 56, 34, 88],
[65, 66, 32, 98],
[43, 87, 65, 43],
[32, 98, 76, 83],
[65, 89, 32, 54],
];
const findMax = arr => {
return arr.map(sub => {
const max = Math.max(...sub);
return max;
});
};
console.log(findMax(arr));
输出
控制台中的输出将是 −
[ 88, 98, 87, 98, 89 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

