1

I'm tryin to convert the column to the row :

 var  input = [
    ["aaa","111","zzz"],
    ["bbb","222","xxx"],
    ["ccc","333","yyy"]
    ];  

to this :

input = [
["aaa","bbb","ccc"],
["111","222","333"],
["zzz","xxx","yyy"],
];

here what i have done, why the output[j].push(input [i][j]); didnt work ?, the output[j] is all I need ... 'j' is looping right ? I did manual, it works , I did all the research but my brain not good enough yet, so can anyone explain it ?? im a total newbie. I need someone to find my mistake since I always thinkin if my logic actually true but why it'snot workin . it make me confused and not able to learn another way.

function dataHandling(){

   var output = []; 

    for (var i=0; i < input.length  ; i++){ 
      output.push([]);
      //console.log(i)
      for (var j =0; j < input[i].length ; j++){
      output[j].push(input[i][j]);
 }

   }
 console.log(output);

}

 // console.log(len2);


     var input = [
["aaa","111","zzz"],
["bbb","222","xxx"],
["ccc","333","yyy"]
];

dataHandling(input);

EDIT : can this apply without passing the parameter ? and keep dataHandling() empty, but thanks for the that answers tho, im tryin to learn it.

EDIT 2 : meanwhile >> output[i].push(input[j][i]); givin me the answer, yeah it's work but if I change unsymmetric matrix if I change the input to:

var input = [
                ["0001", "Roman Alamsyah", "Bandar Lampung", "21/05/1989", "Membaca"],
                ["0002", "Dika Sembiring", "Medan", "10/10/1992", "Bermain Gitar"],
                ["0003", "Winona", "Ambon", "25/12/1965", "Memasak"],
                ["0004", "Bintang Senjaya", "Martapura", "6/4/1970", "Berkebun"]
];

why it's not workin at all ? error in jsbin.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dell Watson
  • 367
  • 1
  • 3
  • 13

4 Answers4

2

You could use two nested Array#forEach lopps and assign the pivot values to the changed indices.

var input = [["aaa", "111", "zzz"], ["bbb", "222", "xxx"], ["ccc", "333", "yyy"]],
    output=[];

input.forEach(function (a, j) {
    a.forEach(function (b, i) {
        output[i] = output[i] || [];
        output[i][j] = b;
    });
});

console.log(output);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can do this with map() you just need to return input[c][i] where

  1. c is index of each element in inner array so 0, 1, 2
  2. i is index of current inner array so in each iteration of elements in inner array it will be 0 then 1 etc ...

var input = [
  ["aaa", "111", "zzz"],
  ["bbb", "222", "xxx"],
  ["ccc", "333", "yyy"]
];

var result = input.map(function(arr, i) {
  return arr.map(function(e, c) {
    return input[c][i]
  })
})

console.log(result)

Your could do it like this if you want to use two for loops.

var input = [
  ["aaa", "111", "zzz"],
  ["bbb", "222", "xxx"],
  ["ccc", "333", "yyy"]
];

var output = [];

for (var i = 0; i < input.length; i++) {
  var ar = [];
  for (var j = 0; j < input[i].length; j++) {
    ar.push(input[j][i]);
  }
  output.push(ar)
}

console.log(output)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

Had modified your solution. Please try it!!

  var input = [["aaa", "111", "zzz"],
                ["bbb", "222", "xxx"],
                ["ccc", "333", "yyy"]
    ];

    function dataHandling() {           
        var output = [];        

        for (var i = 0; i < input.length  ; i++) {                
            output[i] = [];                
            for (var j = 0; j < input[i].length ; j++) {                    
                output[i][j] = input[j][i];
            }
        }        
        console.log(output);
        return output;
    }
J-Mean
  • 1,192
  • 1
  • 8
  • 14
0

While the solutions of Nina and Nenad are very good solutions, I'd like to point out a little problem in your algorithm: your ouput[j].push() is not doing what you're expecting.

Because j is the index inside the sub-array, you will try to push things in other things that does not exists. output will contain only one element, the one you pushed in the beginning of your loop, but you'll try to push something to all 3 elements of output. So you'll call push() on something undefined, resulting in an Error (Uncaught TypeError: Cannot read property 'push' of undefined precisely).

Instead of pushing the elements in that order, I propose you to do it in another way, where you handle first the sub-array of output, instead of the sub-array of input:

function dataHandling(input){
  var output = [];
  for (var i=0; i < input[0].length  ; i++){ 
    output.push([]);
    for (var j =0; j < input.length ; j++)
      output[i].push(input[j][i]);
  }
  console.log(output);
}



var input = [
    ["aaa","111","zzz"],
    ["bbb","222","xxx"],
    ["ccc","333","yyy"]
  ];

dataHandling(input);

In fact it looks a lot like J-Mean answer, but with a little fix on the indices of the two for loops. That function assume that all sub-arrays are of the same length.

EDIT:

About the parameter

In fact you don't need it, if the variable is available in the scope, but I don't remember if it's in the declaration scope, or execution scope. Anyway, not passing it as parameter is a side effect, and it can lead to weird bugs, so I discourage you from using it. At the beginning, it's really tempting to use a lot of global variables (read: variables not passed as parameters), but avoid it as much as possible.

About the new input

I fixed the code above, and tested it, it was the input[i].length which failed, because the sub-arrays were longer than the input, so just replace the i by a 0.

Community
  • 1
  • 1
RallionRl
  • 2,453
  • 2
  • 10
  • 9
  • if the output.push([]); inside the sub-array then output[j] will work right . but I'm still hard to understand it all ..."you will try to push things in other things that does not exists." since in my mind J is count up in sub-array – Dell Watson Nov 14 '16 at 13:02
  • wont work with unsymmetric matrix ? – Dell Watson Nov 14 '16 at 13:11
  • It will work, but only if it is a real matrix, which JavaScript doesn't enforce. Just iterate over your loop, on a paper, and you'll see that in the second for loop, you'll do `ouput[0].push(..)`, `ouput[1].push(..)`, `ouput[2].push(..)`, while `output` will only be of the form: `output = [ [] ]`. `output` won't have anything in indices 1 and 2.That's because `j` iterates over the sub-array, rather than the already existant but not complete `output`. – RallionRl Nov 14 '16 at 15:04
  • But I think that while it's important to understand what are my fixes, IMO the solutions of [Nina](http://stackoverflow.com/a/40588725/5630582) and [Nenad](http://stackoverflow.com/a/40588728/5630582) are way better. – RallionRl Nov 14 '16 at 15:17
  • thank you sir, yeah they're better but not the one i want right now :) , im still newbie and it looks so challenging. – Dell Watson Nov 14 '16 at 22:45