In JavaScript, what is the best way to convert an array of arrays organized as rows into an array of arrays organized as columns

This particular inquiry was quite challenging for me to succinctly summarize in the title, so I do apologize if it may come across as misleading.

Within my possession is an array of arrays structured in the following manner:

array = [ [1,2,3,4,5,6,7,8,9],
          [2,3,4,5,6,7,8,9,1],
          [3,4,5,6,7,8,9,1,2],
          [1,2,3,4,5,6,7,8,9],
          [2,3,4,5,6,7,8,9,1],
          [3,4,5,6,7,8,9,1,2],
          [1,2,3,4,5,6,7,8,9],
          [2,3,4,5,6,7,8,9,1],
          [3,4,5,6,7,8,9,1,2]]

It can be observed that this structure resembles a table with the subarrays essentially representing the rows of said table.

The task at hand involves creating a function capable of taking the primary array and rotating it 90 degrees clockwise. This rotation would result in new subarrays comprising of the first index of each array, followed by the second index of each array, then the third index of each array, and so on. Ultimately, the aim is for the new main array to encompass arrays corresponding to each column within this dataset.

Your assistance would be greatly appreciated!

Answer №1

In order to transform the given multidimensional array, I created a temporary array called newArray. This array will store the resulting multidimensional array, assuming that the main array size and the content-arrays' size are the same as shown in the question example.

var newArray = [];
for(var i = 0; i < array.length; i++) {
    for(var j= 0; j < array.length; j++) {
        newArray[j] = newArray[j] || [];
        newArray[j][i] = array[i][j];
    }
}
console.log(newArray);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting issue with findByIdAndUpdate incorrectly updating nested objects with $set method

I'm currently working on developing a feature that will allow my users to have control over the notifications and emails they receive. My approach involves implementing an object with multiple nested objects to achieve this functionality. While ever ...

Is the original image source revealed when clicked?

I've implemented an expand and collapse feature using jQuery/JavaScript. Clicking on the DIV causes the image inside to change state. However, clicking on the same DIV again doesn't return the image to its original state; it remains stuck in the ...

A guide on importing SVG files into Next.js

I'm in the process of transitioning my React website to Next.js. Currently, I have an assets folder at the root that houses numerous SVGs, along with a constants.js file where I import all the SVGs as ReactComponents. I am seeking guidance on how to a ...

Three.js tutorial: Rendering multiple instances of a single object

var vertgeometry = new THREE.BoxGeometry(75,75,150); var vertmaterial = new THREE.MeshPhongMaterial ( { color: 0xFFFFFF, wireframe: true }); var vert = new THREE.Mesh(vertgeometry, vertmaterial); vert ...

Issue encountered: "An error has occurred stating that your cache folder contains files owned by root. This is likely a result of a bug present in older versions of npm. This issue arose during the execution of the

While attempting to create a new react app using the command npx create-react-app example_app, I encountered the following issue: [ Your cache folder contains root-owned files, due to a bug in previous versions of npm which has since been addressed sudo ...

Creating line breaks for ToolTip titles in Material-UI can be achieved by using the appropriate syntax and

I am currently working with the ToolTip component and I have a query regarding displaying two lines for the title text - one line for each language rather than having them displayed on a single line. Is it possible to achieve this and how can I style the c ...

Can the grunt command be executed automatically after saving code in TypeScript?

As a newcomer to FrontEnd and JavaScript coding in TypeScript, I find myself constantly needing to follow these steps after making a code change: save the code -> compile it using Grunt -> reload the webpage. It can be quite time-consuming. Is there ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

Changing background color using jQuery ID selector

Within thisid, the id of an html element is stored. In order to modify its background color, the code below can be utilized: let thisid = 'test'; $("a#" + thisid).css("background-color", "yellow"); <script src="https://cdnjs.cloudflare.com/a ...

Is there still the possibility to utilize OrbitControls?

Is it possible to continue using OrbitControls in the year 2020? Here is the warning message I received: During the transition to ES6 Modules, the files in 'examples/js' were marked as deprecated in May 2020 (r117) and will be removed in Decembe ...

Loading game resources in advance for future or immediate utilization

I'm currently developing a game UI that involves a large number of image files, totaling around 30MB in size. I've been caching these images to the disk using service workers, but some of them are quite large at 3MB each. Even when they are retri ...

Nested lists with consistent height regardless of the quantity of items

I am currently working on a dropdown multicolumn menu that contains nested lists. My goal is to ensure that all three lists (or columns) have the same height, even if they contain a different number of items. Since the lists are dynamic and the number of i ...

An array of Promise<Employee> cannot be used as a parameter for an array of type Employee[]

I am currently facing an issue with loading my Collection of Employees from a Firestore Database and fetching a 'Workday' for each Employee, which is stored as a Document in a Subcollection named 'Employees'. Below is the code snippet I ...

What is the process for importing Progressbar.js into a Laravel project?

I am attempting to implement a progress bar in Laravel 5.6 using this specific progress bar library. The Progress.js documentation indicates that the following steps are necessary: var ProgressBar = require('progressbar.js') Within the default ...

Unable to transmit information to PHP file using AJAX

I am working with a php file called graph.php. $host = $_POST['hostname']; echo $type=$_POST['type_char']; include('rrdtools.inc.php'); include('graphs/'.$type.'.inc.php'); and I attempting to send data t ...

What is the best way to integrate Angular 5 with various sources of JavaScript code?

I am fairly new to angular. I am looking to merge an external angular script file and on-page javascript code with angular 5. I understand that angular does not typically allow for the inclusion of javascript code in html components, but I believe there m ...

Is there a way to show the contents of local storage on a webpage without having to refresh the page after clicking a button

I am encountering an issue where the contents of local Storage are not being displayed until I manually refresh the page after clicking the submit button. How can I make it so that the changes made in local Storage immediately reflect on the page without n ...

When hovering, the :after element will transition to the current div

How can I make the pink color div move vertically in front of the currently hovered div when we hover on it? Any suggestions on how to achieve this effect? https://i.sstatic.net/yfwgf.png $(document).ready(function() { $(".timeline-entry").hover(fu ...

What could be causing the issue with my css `content:` not functioning across different browsers, and how can I enhance cross-browser compatibility in all cases

Working on my first HTML/CSS project, I encountered a browser compatibility challenge. The code functions well in Firefox, IE, and Edge but fails in Chrome. Specifically, I am trying to make a button display alternating up and down arrows when clicked. Whi ...

How to retrieve elements from a struct array that has been passed in C programming

I'm facing a perplexing issue with my method levenshtein. It populates a 2D array of structs with information and returns a pointer to that array. However, when I pass this array to another method, I encounter a dreaded Segmentation Fault (core dumped ...