

- 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
Transpose of a two-dimensional array - JavaScript
Transpose
The transpose of a matrix (2-D array) is simply a flipped version of the original matrix (2-D array). We can transpose a matrix (2-D array) by switching its rows with its columns.
Let’s say the following is our 2d array −
const arr = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ];
Let’s write the code for this function −
Example
Following is the code −
const arr = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ]; const transpose = arr => { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < i; j++) { const tmp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = tmp; }; } } transpose(arr); console.log(arr);
Output
The output in the console: −
[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
- Related Questions & Answers
- Split one-dimensional array into two-dimensional array JavaScript
- Alternating sum of elements of a two-dimensional array using JavaScript
- Finding transpose of a 2-D array JavaScript
- How to create a two dimensional array in JavaScript?
- Get the Inner product of a One-Dimensional and a Two-Dimensional array in Python
- Passing two dimensional array to a C++ function
- Creating a two-dimensional array with given width and height in JavaScript
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- Multi-Dimensional Array in Javascript
- C# program to Loop over a two dimensional array
- How to declare a two-dimensional array in C#
- What is a two-dimensional array in C language?
- Create new instance of a Two-Dimensional array with Java Reflection Method
- Shift the bits of array elements of a Two-Dimensional array to the left in Numpy
- Shift the bits of array elements of a Two-Dimensional array to the right in Numpy
Advertisements