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?
我有三个数组列使用我正在尝试使用像这样的java脚本创建数组行。我是否需要遍历完整的数据?
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 个解决方案
#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.
现在,您可以使用简单的for循环将内容存储在新的2d数组列中。
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.
在此for循环之后,您将拥有包含每行中按列数据的新数组。
#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.
现在,您可以使用简单的for循环将内容存储在新的2d数组列中。
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.
在此for循环之后,您将拥有包含每行中按列数据的新数组。