いくつか記事を見かけますが・・・・forで回すのが多い。
ですが、簡単に
.reduce((zs, xs) => zs.flat().map(z => xs.map(x => [x, z].flat()))).flat()
これだけで全組み合わせができます
関数で
const combineArrays = (arrays) => arrays.reduce((zs, xs) => zs.flat().map(z => xs.map(x => [x, z].flat()))).flat()
> combineArrays([[1, 2], [3, 4]])
[ [ 3, 1 ], [ 4, 1 ], [ 3, 2 ], [ 4, 2 ] ]
> combineArrays([[1, 2], [3, 4], [5, 6]])
[
[ 5, 3, 1 ],
[ 6, 3, 1 ],
[ 5, 4, 1 ],
[ 6, 4, 1 ],
[ 5, 3, 2 ],
[ 6, 3, 2 ],
[ 5, 4, 2 ],
[ 6, 4, 2 ]
]
ぱっと出てこなかったので、自分のメモ書き程度に。