for-inループ文は、入れ子(ネスト)にして使うこともできる。
多次元の配列(配列型オブジェクト)や連想配列(単純なオブジェクト)の各要素に、何らかの処理を実行したいときに便利だ。
構文
2階層
2次元の配列(配列型オブジェクト)や連想配列(単純なオブジェクト)の各要素に、何らかの処理を実行したいときなど。
for ( 変数A in オブジェクト ) {
繰り返し行う処理文
for ( 変数B in オブジェクト[変数A] ) {
繰り返し行う処理文
}
}
繰り返し行う処理文
for ( 変数B in オブジェクト[変数A] ) {
繰り返し行う処理文
}
}
3階層
3次元の配列(配列型オブジェクト)や連想配列(単純なオブジェクト)の各要素に、何らかの処理を実行したいときなど。
for ( 変数A in オブジェクト ) {
繰り返し行う処理文
for ( 変数B in オブジェクト[変数A] ) {
繰り返し行う処理文
for ( 変数C in オブジェクト[変数A][変数B] ) {
繰り返し行う処理文
}
}
}
繰り返し行う処理文
for ( 変数B in オブジェクト[変数A] ) {
繰り返し行う処理文
for ( 変数C in オブジェクト[変数A][変数B] ) {
繰り返し行う処理文
}
}
}
サンプル
2次元の連想配列(単純なオブジェクト)
<script type="text/javascript">
var $obj = {
propA: {
propA: "値1-1",
propB: "値1-2",
propC: "値1-3"
},
propB: {
propA: "値2-1",
propB: "値2-2",
propC: "値2-3"
},
propC: {
propA: "値3-1",
propB: "値3-2",
propC: "値3-3"
}
};
for ( var $keyA in $obj ) {
for ( var $keyB in $obj[$keyA] ) {
document . write( $obj[$keyA][$keyB] );
document . write( ', ' );
}
document . write( '<br />' );
}
</script>
var $obj = {
propA: {
propA: "値1-1",
propB: "値1-2",
propC: "値1-3"
},
propB: {
propA: "値2-1",
propB: "値2-2",
propC: "値2-3"
},
propC: {
propA: "値3-1",
propB: "値3-2",
propC: "値3-3"
}
};
for ( var $keyA in $obj ) {
for ( var $keyB in $obj[$keyA] ) {
document . write( $obj[$keyA][$keyB] );
document . write( ', ' );
}
document . write( '<br />' );
}
</script>
↓↓↓出力結果↓↓↓
2次元配列(配列型オブジェクト)
<script type="text/javascript">
var $arr = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for ( var $keyA in $arr ) {
for ( var $keyB in $arr[$keyA] ) {
document . write( $arr[$keyA][$keyB] + ', ' );
}
document . write( '<br />' );
}
</script>
var $arr = new Array(
[ "要素1-1", "要素1-2", "要素1-3" ],
[ "要素2-1", "要素2-2", "要素2-3" ],
[ "要素3-1", "要素3-2", "要素3-3" ]
);
for ( var $keyA in $arr ) {
for ( var $keyB in $arr[$keyA] ) {
document . write( $arr[$keyA][$keyB] + ', ' );
}
document . write( '<br />' );
}
</script>
↓↓↓出力結果↓↓↓
3次元配列(配列型オブジェクト)
<script type="text/javascript">
var $arr = 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 ( var $keyA in $arr ) {
for ( var $keyB in $arr[$keyA] ) {
for ( var $keyC in $arr[$keyA][$keyB] ) {
document . write( $arr[$keyA][$keyB][$keyC] + ', ' );
}
document . write( '<br />' );
}
}
</script>
var $arr = 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 ( var $keyA in $arr ) {
for ( var $keyB in $arr[$keyA] ) {
for ( var $keyC in $arr[$keyA][$keyB] ) {
document . write( $arr[$keyA][$keyB][$keyC] + ', ' );
}
document . write( '<br />' );
}
}
</script>
↓↓↓出力結果↓↓↓