- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Build maximum array based on a 2-D array - JavaScript
Let’s say, we have an array of arrays of Numbers like below −
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 the above array, the output should be −
const output = [ 48, 98, 65, 83, 89 ];
Example
Following is the code to get the greatest element from each subarray −
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
This will produce the following output in console −
[ 48, 98, 65, 98, 89 ]
- Related Articles
- Constructing 2-D array based on some constraints in JavaScript
- Sorting Array based on another array JavaScript
- Sort array based on another array in JavaScript
- Modify an array based on another array JavaScript
- Filter array based on another array in JavaScript
- Sort object array based on another array of keys - JavaScript
- Reorder array based on condition in JavaScript?
- Build tree array from flat array in JavaScript
- Get range of months from array based on another array JavaScript
- Filter an object based on an array JavaScript
- Search and update array based on key JavaScript
- Shuffling string based on an array in JavaScript
- Order an array of words based on another array of words JavaScript
- Creating an array of objects based on another array of objects JavaScript
- Shifting string letters based on an array in JavaScript

Advertisements