Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Created September 17, 2019 07:40
Show Gist options
  • Save mytharcher/a8aedc8ca066937cf5316b7178a0acb1 to your computer and use it in GitHub Desktop.
Save mytharcher/a8aedc8ca066937cf5316b7178a0acb1 to your computer and use it in GitHub Desktop.
计算任意多个维度有限元素生成的所有向量组合
const lists = [
[1, 2],
[3, 4],
[5],
[7]
];
const combinations = lists
.reduce((all, current) => current
.map(item => all.map(g => g.concat([item])))
.reduce((prev, curr) => prev.concat(curr), [])
, [[]]);
console.log(combinations);
/*
[
[ 1, 3, 5, 7 ],
[ 2, 3, 5, 7 ],
[ 1, 4, 5, 7 ],
[ 2, 4, 5, 7 ]
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment