
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript: Combine highest key values of multiple arrays into a single array
We are required to write a JavaScript function that takes in any number of array of Numbers. Our function should return an array of greatest numbers picked from the input array of array. The number of elements in the output array should be equal to the number of subarrays contained in the original input array.
Example
The code for this will be −
const arr1 = [117, 121, 18, 24]; const arr2 = [132, 19, 432, 23]; const arr3 = [32, 23, 137, 145]; const arr4 = [900, 332, 23, 19]; const mergeGreatest = (...arrs) => { const res = []; arrs.forEach(el => { el.forEach((elm, ind) => { if(!( res[ind] > elm)) { res[ind] = elm; }; }); }); return res; }; console.log(mergeGreatest(arr1, arr2, arr3, arr4));
Output
And the output in the console will be −
[ 900, 332, 432, 145 ]
- Related Articles
- Combine unique items of an array of arrays while summing values - JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- How to get single array from multiple arrays in JavaScript
- How to sum elements at the same index in array of arrays into a single array? JavaScript
- Inserting multiple parameter values into a single column with MySQL?
- How to combine 2 arrays into 1 object in JavaScript
- How to return object from an array with highest key values along with name - JavaScript?
- Combining multiple images into a single one using JavaScript
- Turning a 2D array into a sparse array of arrays in JavaScript
- Aggregate multiple arrays into one huge array with MongoDB?
- Join Map values into a single string with JavaScript?
- JavaScript Converting array of objects into object of arrays
- JavaScript Array: Checking for multiple values
- Converting array of arrays into an object in JavaScript
- Split Array of items into N Arrays in JavaScript

Advertisements