Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have read this To swap rows with columns of matrix in javascript (or jquery) 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
share|improve this question
1  
What is this in this context? –  Mathletics Aug 13 '13 at 20:17
    
Show the this.fields structure. –  DontVoteMeDown Aug 13 '13 at 20:18
    
As described above this is 2-x array in fields[i].value[j]. Just I want to how enforce JS write a value to arbitrary index of array. –  kostya Aug 13 '13 at 20:42
    
@kostya sorry, can't see where it is described on post above. –  DontVoteMeDown Aug 13 '13 at 20:53

2 Answers 2

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];

        }

    }
share|improve this answer

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];

    }

}
share|improve this answer
    
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 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.