Skip to content

Instantly share code, notes, and snippets.

@fazlurr
Last active July 8, 2018 16:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fazlurr/264adb3cefb663c37b5401a6042f1b68 to your computer and use it in GitHub Desktop.
JS Create Combinations or Cartesian Product - https://stackoverflow.com/a/50631472/5627904
const result = data.reduce(
(a, b) => a.reduce(
(r, v) => r.concat(b.map(w => [].concat(v, w))),
[]
)
);
var data = [[1, 2], [10, 20], [100, 200, 300]],
result = data.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
console.log(result.map(a => a.join(' ')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment