array.reduce( callback )メソッドを使い、二次元配列を一次元配列にまとめる方法。
実装例
ソースコード
JavaScript
<script type="text/javascript">
function toOneDimension( $previousValue, $currentValue ) {
return $previousValue.concat( $currentValue );
}
var $sampleArrayA = [
[ 'A1', 'A2', 'A3' ],
[ 'B1', 'B2', 'B3' ],
[ 'C1', 'C2', 'C3' ]
];
var $sampleArrayB = $sampleArrayA.reduce( toOneDimension );
for ( var $counterVar = 0; $counterVar < $sampleArrayB.length; $counterVar++ ) {
document.write( '$sampleArrayB[' +$counterVar +']: ' );
document.write( $sampleArrayB[$counterVar] );
document.write( '<br />' );
}
</script>
function toOneDimension( $previousValue, $currentValue ) {
return $previousValue.concat( $currentValue );
}
var $sampleArrayA = [
[ 'A1', 'A2', 'A3' ],
[ 'B1', 'B2', 'B3' ],
[ 'C1', 'C2', 'C3' ]
];
var $sampleArrayB = $sampleArrayA.reduce( toOneDimension );
for ( var $counterVar = 0; $counterVar < $sampleArrayB.length; $counterVar++ ) {
document.write( '$sampleArrayB[' +$counterVar +']: ' );
document.write( $sampleArrayB[$counterVar] );
document.write( '<br />' );
}
</script>