Skip to content

Instantly share code, notes, and snippets.

@msr1k
Last active August 28, 2020 05:40
Show Gist options
  • Save msr1k/7b6b86c4272c276660951bc8e33cff06 to your computer and use it in GitHub Desktop.
Save msr1k/7b6b86c4272c276660951bc8e33cff06 to your computer and use it in GitHub Desktop.
Get product result of the given arrays (JavaScript)
const a = [ 1, 2, 3, 4 ];
const b = [ 5, 6, 7 ];
const c = [ 8, 9 ];
const d = { a, b, c }
function combinations(aryOfAry) {
const combination = (a1, a2) => {
let a = []
a1.forEach((v1) => {
a2.forEach((v2) => {
let t = v1.slice();
t.push(v2);
a.push(t);
});
});
return a;
}
let base = [[]];
aryOfAry.forEach((v) => {
base = combination(base, v);
});
return base;
}
console.log(combinations(Object.values(d)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment