We have an array of arrays of Numbers like this one −
const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ];
We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray.
So, for this array, the output should be −
const output = [ 48, 98, 65, 83, 89 ];
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [
[1, 16, 34, 48],
[6, 66, 2, 98],
[43, 8, 65, 43],
[32, 98, 76, 83],
[65, 89, 32, 4],
];
const constructBig = arr => {
return arr.map(sub => {
const max = Math.max(...sub);
return max;
});
};
console.log(constructBig(arr));Output
The output in the console will be −
[ 48, 98, 65, 98, 89 ]