使用 map JavaScript 返回每个数组中的最大数
我们有一个包含数字的数组数组,如下所示 −
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 ]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP