Wrapping one's head around multi-dimensional arrays is confusing enough by itself (compare the comments above about square vs. non-square), but there's a bit more going on here than was previously explained.
Consider the inner part: rows.map( row => row[columnIndex] ).
This is straightforward: for each row we extract the value in the specified column position, so this makes a 1-dimensional array representing a column in the original array ("rows") - e.g. if columnindex is 2 this returns [9, 4, 800]. Note that "rows" is being referenced here as a variable external to the map function, whereas "row" is just an internal name of the map function referring to the array element being processed, and the argument "columnIndex" is being passed in from the outer map() call.
Now the outer map function is being applied to the elements of rows[0] (and NOT to rows itself!), meaning it takes that first row (simply to determine how many columns there are!) and walks through it, disregarding the current value completely (that's why the generic underscore is used) and only caring about the current position in that array, i.e. the columnIndex. For each column index it will return the output of that inner map we just discussed.
To be clear, this works just fine on any rectangular array: if your original array had had 3 rows of 4 values each, the outer map would have walked through those 4 positions, and for each of them returned an array of 3 values extracted from the 3 rows at that given position, so the resulting array would have had 4 rows of 3 values each, perfectly transposing the original matrix.
2D computer graphics is the computer-based generation of digital images—mostly from two-dimensional models.
- totally not what you're talking about – Interstellarmap
does yet? – Cutwaterrows[0].map((_, columnIndex) => rows.map(row => row[columnIndex]))
. – Standford