多次元配列を作り、変数に代入

多次元配列を作り、変数に代入する方法と、多次元配列の要素にアクセスする方法。

解説

多次元配列を作る

2次元配列を作った要領で、次元を増やしていけば、多次元配列を作ることができる。

[]で括る方法

<script type="text/javascript">
var sampleArray = [
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
];
</script>

new Array()

<script type="text/javascript">
var sampleArray = new Array(
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
);
</script>

Array()

<script type="text/javascript">
var sampleArray = Array(
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
);
</script>

多次元配列の要素にアクセス

インデックスを複数指定すれば、多次元配列の要素にアクセスできる。

<script type="text/javascript">
sampleArray[0][0][0] // 要素1-1-1
sampleArray[0][2][2] // 要素1-3-3
sampleArray[1][0][1] // 要素2-1-2
sampleArray[2][1][2] // 要素3-2-3
</script>

サンプル

[]で括る方法

ソースコード

<script type="text/javascript">
var sampleArray = [
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
];
for( indexA = 0; indexA < sampleArray . length; indexA++ ){
    for( indexB = 0; indexB < sampleArray[indexA] . length; indexB++ ){
        document . write( sampleArray[indexA][indexB][0] + " : " );
        document . write( sampleArray[indexA][indexB][1] + " : " );
        document . write( sampleArray[indexA][indexB][2] + "<br />" );
    }
}
</script>

new Array()

ソースコード

<script type="text/javascript">
var sampleArray = new Array(
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
);
for( indexA = 0; indexA < sampleArray . length; indexA++ ){
    for( indexB = 0; indexB < sampleArray[indexA] . length; indexB++ ){
        document . write( sampleArray[indexA][indexB][0] + " : " );
        document . write( sampleArray[indexA][indexB][1] + " : " );
        document . write( sampleArray[indexA][indexB][2] + "<br />" );
    }
}
</script>

Array()

ソースコード

<script type="text/javascript">
var sampleArray = Array(
    [
        [ "要素1-1-1", "要素1-1-2", "要素1-1-3" ],
        [ "要素1-2-1", "要素1-2-2", "要素1-2-3" ],
        [ "要素1-3-1", "要素1-3-2", "要素1-3-3" ]
    ],
    [
        [ "要素2-1-1", "要素2-1-2", "要素2-1-3" ],
        [ "要素2-2-1", "要素2-2-2", "要素2-2-3" ],
        [ "要素2-3-1", "要素2-3-2", "要素2-3-3" ]
    ],
    [
        [ "要素3-1-1", "要素3-1-2", "要素3-1-3" ],
        [ "要素3-2-1", "要素3-2-2", "要素3-2-3" ],
        [ "要素3-3-1", "要素3-3-2", "要素3-3-3" ]
    ]
);
for( indexA = 0; indexA < sampleArray . length; indexA++ ){
    for( indexB = 0; indexB < sampleArray[indexA] . length; indexB++ ){
        document . write( sampleArray[indexA][indexB][0] + " : " );
        document . write( sampleArray[indexA][indexB][1] + " : " );
        document . write( sampleArray[indexA][indexB][2] + "<br />" );
    }
}
</script>

スポンサード リンク

カテゴリー: JavaScript, リファレンス, 変数 パーマリンク