2次元配列を作り、変数に代入する方法と、2次元配列の要素にアクセスする方法。
解説
2次元配列を作る
1次元配列の要素の値を、配列にするだけで、2次元配列を作ることができる。
[]で括る方法
<script type="text/javascript">
var sampleArray = [
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
];
</script>
var sampleArray = [
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
];
</script>
new Array()
<script type="text/javascript">
var sampleArray = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
</script>
var sampleArray = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
</script>
Array()
<script type="text/javascript">
var sampleArray = Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
</script>
var sampleArray = Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
</script>
2次元配列の要素にアクセス
インデックスを2つ指定すれば、2次元配列の要素にアクセスできる。
<script type="text/javascript">
sampleArray[0][0] // 要素1-1
sampleArray[0][2] // 要素1-3
sampleArray[1][0] // 要素2-1
sampleArray[2][1] // 要素3-2
</script>
sampleArray[0][0] // 要素1-1
sampleArray[0][2] // 要素1-3
sampleArray[1][0] // 要素2-1
sampleArray[2][1] // 要素3-2
</script>
サンプル
[]で括る方法
ソースコード
<script type="text/javascript">
var sampleArray = [
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
];
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>
var sampleArray = [
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
];
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>
new Array()
ソースコード
<script type="text/javascript">
var sampleArray = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>
var sampleArray = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>
Array()
ソースコード
<script type="text/javascript">
var sampleArray = Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>
var sampleArray = Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for( index = 0; index < sampleArray . length; index++ ){
document . write( sampleArray[index][0] + " : " );
document . write( sampleArray[index][1] + " : " );
document . write( sampleArray[index][2] + "<br />" );
}
</script>