
- 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
Reshaping 2-D array in JavaScript
Problem
We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the first argument and two numbers, r and c, representing the row number and column number of the desired matrix, respectively.
Our function should form and return a new 2-D array with the specified rows and columns in the same row-traversing order as they were in the input array.
For example, if the input to the function is −
const arr = [ [6, 7], [8, 9] ]; const r = 1, c = 4;
Then the output should be −
const output = [[6, 7, 8, 9]];
Output Explanation
The row-traversing of arr is [6, 7, 8, 9]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list
Example
Following is the code −
const arr = [ [6, 7], [8, 9] ]; const r = 1, c = 4; const reshapeArray = (arr, r, c) => { if (r * c !== arr.length * arr[0].length) { return arr } const res = [] let row = [] arr.forEach(items => items.forEach((num) => { row.push(num) if (row.length === c) { res.push(row) row = [] } })) return res }; console.log(reshapeArray(arr, r, c));
Output
Following is the console output −
[[6, 7, 8, 9]]
- Related Articles
- Grouping and sorting 2-D array in JavaScript
- Converting object to 2-D array in JavaScript
- Searching in a sorted 2-D array in JavaScript
- Nth smallest element in sorted 2-D array in JavaScript
- Build maximum array based on a 2-D array - JavaScript
- Finding transpose of a 2-D array JavaScript
- Constructing 2-D array based on some constraints in JavaScript
- Sorting only columns of a 2-D array in JavaScript
- Counting largest numbers in row and column in 2-D array in JavaScript
- Emulating a 2-d array using 1-d array in C++
- How SaaS Solutions are Reshaping Modern Workplace
- Sorting 2-D array of strings and finding the diagonal element using JavaScript
- Finding the sum of minimum value in each row of a 2-D array using JavaScript
- How to flatten an input tensor by reshaping it in PyTorch?
- Print a 2 D Array or Matrix in C#

Advertisements