Skip to content

Instantly share code, notes, and snippets.

@HeGanjie
Created November 29, 2018 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeGanjie/1b91d7894b2a76f51a3be5da419954ca to your computer and use it in GitHub Desktop.
Save HeGanjie/1b91d7894b2a76f51a3be5da419954ca to your computer and use it in GitHub Desktop.
// in: [['xxx'], ['yyy', 'zzz'], ...]
// out: [['xxx', 'yyy', ...], ['xxx', 'zzz', ...]]
function calcCombination(options) {
if (_.isEmpty(options)) {
return [[]]
}
let [firstOpt, ...rest] = options
let remainCombinations = calcCombination(rest)
return _.flatMap(remainCombinations, rc => {
return firstOpt.map(o1 => [o1, ...rc])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment