13

I'm trying to create a function that takes in any array and transpose it so that rows turns to columns and columns to rows.

Not sure what I've done wrong or missing but keep getting this message once the array is pass through....

TypeError: Cannot set property "0.0" of undefined to "xxxxxx".

the error is on line

result[row][col] = array[col][row]; // Rotate

Any pointer would be much appreciated.

function transposeArray(array){
        var result = [];
        for(var row = 0; row < array.length; row++){ // Loop over rows
          for(var col = 0; col < array[row].length; col++){ // Loop over columns
            result[row][col] = array[col][row]; // Rotate
          }
        }
        return result;
    }
Rubén
  • 34,714
  • 9
  • 70
  • 166
user1488934
  • 247
  • 2
  • 5
  • 13
  • Related: [Reference : TypeError: Cannot read property \[property name here\] from undefined](https://stackoverflow.com/q/74109026/1595451) – Rubén Oct 29 '22 at 12:02

6 Answers6

23

My personal favourite is this gist:

function transpose(a)
{
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
5

You will need to initially assign an array object to each element in the outer result array. There might be more efficient ways, but I would be thinking:

function transposeArray(array){
  var result = [];
  for (var col = 0; col < array[0].length; col++) { // Loop over array cols
    result[col] = [];
    for (var row = 0; row < array.length; row++) { // Loop over array rows
      result[col][row] = array[row][col]; // Rotate
    }
  }
  return result;
}
AdamL
  • 23,691
  • 6
  • 68
  • 59
1

I have found this way:

function onOpen() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("SEGUNDOA2E");
    var notas = sheet.getRange("G6:AK6").getNotes();

    for (var i = 0; i < 31; i++){
        var nota = notas[0][i];
        var conceptos = sheet.getRange(37+i,7).setValue(nota);
    } 
}

Hope this helps.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
1

AdamL and Mogsdad solutions are great, but for completeness, I just wanted to share another solution I came up with before finding this question:

function transpose2DArray(a)
{
  return Array(a).map(r=> r.map(cell => cell[0]))
}

I found this after noticing that setting transposed values with this works:

var a2 = []
a2[0] = a
sheet.getRange(1, 1, a2.length, a2[0].length).setValues(arr2)
// or
//sheet.getRange(1, 1, a[0].length, a.length).setValues(Array(a))

which actually puts arrays with a single element in cells. My function use the same trick but also converts elements of the 2D array, which are arrays with a single element, to their inner element (and so removing a nested level)

hymced
  • 570
  • 5
  • 19
0

I solved with request function formula:

  var RanMar = HojaResp.getRange("H1:X1"); // Se establece rango para las marcas de cemento.
  var Rango2 = HojaInfo.setActiveCell("B3"); // Temporal
  Rango2.setFormula('=transpose(Respuestas!H1:X1)');

Unorthodox, but functional for me.

Rubén
  • 34,714
  • 9
  • 70
  • 166
0

if it can help

x = [1, 3]

it'll return :

1
3

if you want that it return

1 3

you should do : [[1, 3]]