0

i have three array column wise using which i am trying to create array row wise using java-script like this. Do i need to traverse through complete data?

what i have 

var a=new Array("1","2","3","4");

var b=new Array("5","6","7","8");

var c=new Array("9","10","11","12");


what i want to create using the above array

var d=new Array("1","5","9");

var e=new Array("2","6","10");

var f=new Array("3","7","11");

var g=new Array("4","8","12");

1 Answer 1

1

If you are denoting each array by different variable names, then the task becomes tedious. On the other hand, if you store them in a single 2-Dimensional array it can be accomplished easily.

Say the first array is alternatively declared as follows:

var total_array = new Array(
                   new Array("1","2","3","4"),
                   new Array("5","6","7","8"),
                   new Array("9","10","11","12")
                  );

Now you can use a simple for loop to store the contents in a new 2d array column wise.

var new_array = new Array(4);
for(var i=0; i<4; i++)
      new_array[i] = new Array(3);

//now copy the contents...

for(var i=0; i<3; i++)
{
    for(var j=0; j<4; j++)
    {
         new_array[j][i] = total_array[i][j];
    }
}

And after this for loop you have the new array containing the column-wise data in each row.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.