export const descartes = <T>(array: T[][]) => {
if (array.length < 2) {
return array[0]?.map(item => [item]) || [];
}
return Array.prototype.reduce.call(array, (col: (T | T[])[], set: T[]) => {
let res: T[][] = [];
col.forEach((c) => {
set.forEach((s) => {
let t: T[] = Array.isArray(c)? [...c] : [c]
t.push(s);
res.push(t);
})
});
return res;
});
}
参考
export const descartes = <T>(array: T[][]) => {
return array.reduce((prev, cur) => {
let res: T[][] = [];
prev.forEach(p => cur.forEach(c => arr.push(p.concat([c]))))
return res;
}, [[]] as T[][])
}
参考