日期:2014-05-16  浏览次数:20530 次

js中如何实现二维数组的转置。。。
如下代码为什么不能实现二维数组的转置??

<html>
<body>
<script language="javascript">
var a=[[5,4,5,4,5,5,1],['fd',45,3.6,7,22,56,7],[4,2,5,'gshshgjh',45,525,2]];
var b=[];

for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
b[j][i]=a[i][j];
}
}
for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
document.writeln(j[i][j]);
}
document.writeln("<br/>");
}

</script>
</body>

</html>

------解决方案--------------------
<html>
<body>
<script language="javascript">
var a = [[5, 4, 5, 4, 5, 5, 1], ['fd', 45, 3.6, 7, 22, 56, 7], [4, 2, 5, 'gshshgjh', 45, 525, 2]];
var b = [];

for (var i = 0; i < a.length; i++) {
b[i] = [];
for (var j = 0; j < a[i].length; j++) {
b[i][j] = a[i][j];
}
}
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < a[i].length; j++) {
document.writeln(b[i][j]);
}
document.writeln("<br/>");


</script>
</body> 

</html>
------解决方案--------------------
<html>
<body>
<script language="javascript">
var a=[[5,4,5,4,5,5,1],['fd',45,3.6,7,22,56,7],[4,2,5,'gshshgjh',45,525,2]];
var b=[];

for(var i=0;i<a.length;i++){
b[i] = [];
for(var j=0;j<a[i].length;j++){
b[i][j] = a[i][j];
}
}
for(var i=0;i<a.length;i++){ 
for(var j=0;j<a[i].length;j++){
document.writeln(b[i][j]);
}
document.writeln("<br/>");


</script>
</body> 

</html>


这个意思?
------解决方案--------------------
探讨

没有实现效果啊


效果是新的二维数组是旧的二维数组的行和列交换。。。。

------解决方案--------------------
探讨

行和列的值交换。。。

------解决方案--------------------
我想我明白你意思了,是不是下面这样的?
<html>
<body>
<script language="javascript">
var a = [[5, 4, 5, 4, 5, 5, 1], ['fd', 45, 3.6, 7, 22, 56, 7], [4, 2, 5, 'gshshgjh', 45, 525, 2]];
var b = [];
for (var j = 0; j < a[0].length; j++) {
b[j] = [];
for (var i = 0; i < a.length; i++) {

b[j][i] = a[i][j];
}

}
for (var i = 0; i < b.length; i++) {
for (var j = 0; j < b[i].length; j++) {
document.writeln(b[i][j]);
}
document.writeln("<br/>");
}

</script>
</body>

</html>