-1

I have read this Swap rows with columns (transposition) of a matrix in javascript However, it did not work for me (because I still stupi).

There are numbers of arrays each as individual colum like that:

id [1, 2, 3]
caption [one, two, three]
title [One, Two, Three]

I want to convert columns to row:

arr= [1, one, One]

...

Some code

        var res = [];

        for(i in this.fields) {

            for(j in this.fields[i].value) {

                res[j][i] = this.fields[i].value[j];

            }

        }

it give me "TypeError: can't convert undefined to object "

In php this method works fine, but could somebody point me how to do it in js. Thanks in advance.

UPDATE for simplication

var arr = [];
arr[0] = [];

arr[6][0] = 5;
/*
Exception: can't convert undefined to object
@Scratchpad/1:4
 */

When we scan common string we iterate with indexes like 0-0, 0-1, 0-2 until end-of-row when starts again 1-0 and so on. But here I need 0-0, 1-0, 2-0 end of col and again 1-0, 1-1, 1-1 ...

UPDATE for "this". Just add a cople of lines:

                console.log(this.fields[i].value[j]);
                console.log('indexes: i='+i, 'j='+j);

and as could you see there are no undefined values

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
Community
  • 1
  • 1
kostya
  • 209
  • 3
  • 11

2 Answers2

1

There are a few mistakes in your source code. We don´t know how your this.fields value looks like, but for the sake of your code snippet let it look like that:

this.fields = [
    { value: [1, 2, 3] },
    { value: [4, 5, 6] },
    { value: [7, 8, 9] }
]

If your this.fields variable looks like that, you are not so far away from the solution. Your error message says TypeError: can't convert undefined to object, so I am guessing your variable does not look like described.

When we transform your data in a form that looks like my example, your are not so far away from the solution in your code snippet. The problem is res does not know, that its second dimension is supposed to consist of arrays, because you never defined that. We can fix that by adding if(i === 0) res[j] = [];.

So with the described structure and our little fix, it should work:

    var res = [];

    for(i in this.fields) {

        for(j in this.fields[i].value) {

            if(i === 0) res[j] = [];
            res[j][i] = this.fields[i].value[j];

        }

    }
Amberlamps
  • 39,180
  • 5
  • 43
  • 53
0

For sure one error is within the loop itself. On the first iteration res[j] exists, but inside the inner loop res[j][i] is not defined and will throw an error. You could fix it by checking if the element already exists:

var res = [];

for(i in this.fields) {

    for(j in this.fields[i].value) {

        if( !res[j] ) { res[j] = []; }

        res[j][i] = this.fields[i].value[j];

    }

}
danielepolencic
  • 4,905
  • 1
  • 26
  • 25
  • Thanks. So I did, but without condition and getting all elements empty except the last one. Thanks again. Now it works. – kostya Aug 14 '13 at 03:12