

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Picking the largest elements from multidimensional array in JavaScript
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 ]
- Related Questions & Answers
- Picking index randomly from array in JavaScript
- Picking out uniques from an array in JavaScript
- Constructing largest number from an array in JavaScript
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array in C++
- Single dimensional array vs multidimensional array in JavaScript.
- Picking the odd one out in JavaScript
- Picking all elements whose value is equal to index in JavaScript
- Find the average of all elements of array except the largest and smallest - JavaScript
- Find the largest three elements in an array in C++
- Maximum area rectangle by picking four sides from array in C++
- Return the largest array between arrays JavaScript
- Maximum sum by picking elements from two arrays in order in C++ Program
- PHP Multidimensional Array.
- Remove elements from array using JavaScript filter - JavaScript
- How to convert Multidimensional PHP array to JavaScript array?
Advertisements