
- 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
Splitting an array based on its first value - JavaScript
Suppose we have an array of arrays of numbers like this −
const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];
Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all second elements of the subarrays that have similar first value are grouped together.
Therefore, for the array above, the output should look like −
const output = [ [45, 34, 49], [34, 67], [78, 65] ];
We can make use of the Array.prototype.reduce() method that takes help of a Map() to construct the required array.
Example
Following is the code −
const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; const constructSimilarArray = (arr = []) => { const creds = arr.reduce((acc, val) => { const { map, res } = acc; if(!map.has(val[0])){ map.set(val[0], res.push([val[1]]) - 1); }else{ res[map.get(val[0])].push(val[1]); }; return { map, res }; }, { map: new Map(), res: [] }); return creds.res; }; console.log(constructSimilarArray(arr));
Output
This will produce the following output in console −
[ [ 45, 34, 49 ], [ 34, 67 ], [ 78, 65 ] ]
- Related Articles
- Splitting strings based on multiple separators - JavaScript
- Modify an array based on another array JavaScript
- Filter an object based on an array JavaScript
- Splitting an array into chunks in JavaScript
- Splitting an array into groups in JavaScript
- Shuffling string based on an array in JavaScript
- Shifting string letters based on an array in JavaScript
- Splitting an object into an array of objects in JavaScript
- Display resultant array based on the object’s order determined by the first array in JavaScript?
- Sorting Array based on another array 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
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- Delete duplicate elements based on first letter – JavaScript
- Grouping names based on first letter in JavaScript

Advertisements