Skip to content

Instantly share code, notes, and snippets.

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 norbornen/0565f353907d812ee1f24ba546d5f0fd to your computer and use it in GitHub Desktop.
Save norbornen/0565f353907d812ee1f24ba546d5f0fd to your computer and use it in GitHub Desktop.
const values = [1,2,null,3].map((x) => !!x || null);
const vlength = values.length;
let combinations = [];
for (let i = 0; i < vlength; i++) {
if (values[i]) {
let acc = [];
acc.push(new Array(vlength).fill(null).fill(true, i, i + 1));
for (let j = vlength - 1; j > i; j--) {
if (values[j]) {
acc = acc.concat(
acc.map((x) => x.slice().fill(true, j, j + 1)));
}
}
combinations = combinations.concat(acc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment