あなたが正しく質問していることを私が理解したことを考えると、あなたはすべてのサブ配列のデカルト積を求めています。
可能な限り最も愚かなバージョンを構築することから始めましょう。賢さはなく、ダブルループだけです。
function product ( left, right ) {
var ret = [];
for (var i = 0; i < left.length; i++) {
for (var j = 0; j < right.length; j++) {
ret.push( [left[i], right[j]] );
}
}
return ret;
}
私たちは単に製品の定義に従います:のすべてのアイテムleft
に対してのすべてのアイテムright
。少し賢くしましょう。両方の配列を反復処理し、左側の各項目を右側の項目に対してマッピングします。多分私たちが使用する場合Array#map
...
function product ( left, right ) {
return left.map(function ( i ) {
return right.map(function ( j ) {
return [i, j];
});
});
}
ただし、これによりネストされた配列が作成されます。
> product( [0, 1], [2, 3] );
[ [[0,2], [0,3]],
[[1,2], [1,3]] ]
最初の配列を減らすことでそれを解決できます。
function product ( left, right ) {
return left.reduce(function ( ret, i ) {
var ans = right.map(function ( j ) {
return [i, j];
});
return ret.concat( ans );
}, []);
}
これまでのところ、これは製品について説明しましたleft × right
が、n-ary製品が必要でした:A × B × C × ...
どのようにそれを行いますか?
デカルト積の非常に有用な特性を使用しますA × B × C = A × (B × C)
。つまり、n-ary積を再帰的に定義できるということです。これが最初の試みです:
function product ( left, right, other ) {
if ( other ) {
right = product.apply( this, [].slice.call(arguments, 1) );
}
return left.reduce(function ( ret, i ) {
var ans = right.map(function ( j ) {
return [i, j];
});
return ret.concat( ans );
}, []);
}
エラーは明確である必要があります:[i, j]
。これは、実際にスカラーと配列がある場合に、2つのスカラー(通常のフラット値)があることを前提としています。修正は簡単です。結果を配列でラップする代わりに、配列をスカラーに連結して、次の最終的な解決策を導き出します。
function product ( left, right, other ) {
if ( other ) {
right = product.apply( this, [].slice.call(arguments, 1) );
}
return left.reduce(function ( ret, i ) {
var ans = right.map(function ( j ) {
return [i].concat( j );
});
return ret.concat( ans );
}, []);
}
あなたの場合、配列の配列があるので、呼び出すときはそれをフラット化する必要があります:
> var arr = [ ["a1", "a2"], ["b1", "b2", "b3"], ["c1", "c2"], ["d1", "d2", "d3", "d4"] ];
undefined
> product.apply( null, arr );
[ [ 'a1', 'b1', 'c1', 'd1' ],
[ 'a1', 'b1', 'c1', 'd2' ],
[ 'a1', 'b1', 'c1', 'd3' ],
[ 'a1', 'b1', 'c1', 'd4' ],
[ 'a1', 'b1', 'c2', 'd1' ],
[ 'a1', 'b1', 'c2', 'd2' ],
[ 'a1', 'b1', 'c2', 'd3' ],
[ 'a1', 'b1', 'c2', 'd4' ],
[ 'a1', 'b2', 'c1', 'd1' ],
[ 'a1', 'b2', 'c1', 'd2' ],
[ 'a1', 'b2', 'c1', 'd3' ],
[ 'a1', 'b2', 'c1', 'd4' ],
[ 'a1', 'b2', 'c2', 'd1' ],
[ 'a1', 'b2', 'c2', 'd2' ],
[ 'a1', 'b2', 'c2', 'd3' ],
[ 'a1', 'b2', 'c2', 'd4' ],
[ 'a1', 'b3', 'c1', 'd1' ],
[ 'a1', 'b3', 'c1', 'd2' ],
[ 'a1', 'b3', 'c1', 'd3' ],
[ 'a1', 'b3', 'c1', 'd4' ],
[ 'a1', 'b3', 'c2', 'd1' ],
[ 'a1', 'b3', 'c2', 'd2' ],
[ 'a1', 'b3', 'c2', 'd3' ],
[ 'a1', 'b3', 'c2', 'd4' ],
[ 'a2', 'b1', 'c1', 'd1' ],
[ 'a2', 'b1', 'c1', 'd2' ],
[ 'a2', 'b1', 'c1', 'd3' ],
[ 'a2', 'b1', 'c1', 'd4' ],
[ 'a2', 'b1', 'c2', 'd1' ],
[ 'a2', 'b1', 'c2', 'd2' ],
[ 'a2', 'b1', 'c2', 'd3' ],
[ 'a2', 'b1', 'c2', 'd4' ],
[ 'a2', 'b2', 'c1', 'd1' ],
[ 'a2', 'b2', 'c1', 'd2' ],
[ 'a2', 'b2', 'c1', 'd3' ],
[ 'a2', 'b2', 'c1', 'd4' ],
[ 'a2', 'b2', 'c2', 'd1' ],
[ 'a2', 'b2', 'c2', 'd2' ],
[ 'a2', 'b2', 'c2', 'd3' ],
[ 'a2', 'b2', 'c2', 'd4' ],
[ 'a2', 'b3', 'c1', 'd1' ],
[ 'a2', 'b3', 'c1', 'd2' ],
[ 'a2', 'b3', 'c1', 'd3' ],
[ 'a2', 'b3', 'c1', 'd4' ],
[ 'a2', 'b3', 'c2', 'd1' ],
[ 'a2', 'b3', 'c2', 'd2' ],
[ 'a2', 'b3', 'c2', 'd3' ],
[ 'a2', 'b3', 'c2', 'd4' ] ]