I came across a solution on Stack Overflow about swapping rows with columns in a matrix using JavaScript. However, it didn't work for me (probably because I'm still learning).
The data is structured in arrays like:
id [1, 2, 3]
caption [one, two, three]
title [One, Two, Three]
My goal is to convert these arrays into rows:
arr= [1, one, One]
Unfortunately, when trying the following code snippet:
var res = [];
for(i in this.fields) {
for(j in this.fields[i].value) {
res[j][i] = this.fields[i].value[j];
}
}
I encounter an error "TypeError: can't convert undefined to object."
In PHP, a similar method works fine, but I need assistance in achieving this functionality in JavaScript. Thank you in advance.
UPDATE for simplification:
var arr = [];
arr[0] = [];
arr[6][0] = 5;
/*
Exception: can't convert undefined to object
@Scratchpad/1:4
*/
Typically, we iterate through strings using indexes like 0-0, 0-1, 0-2 until the end of a row before moving to the next row. But in my case, I require iterating through columns first - 0-0, 1-0, 2-0 before moving to the end of a column and then repeating the pattern.
UPDATE regarding "this". By adding a couple of lines:
console.log(this.fields[i].value[j]);
console.log('indexes: i='+i, 'j='+j);
You'll notice that there are no undefined values present.
4
indexes: i=0 j=0
1
indexes: i=1 j=0
1
indexes: i=2 j=0
one
indexes: i=3 j=0
One
indexes: i=4 j=0