Skip to content

Instantly share code, notes, and snippets.

@PavelJurasek
Created May 7, 2019 16:59
Show Gist options
  • Save PavelJurasek/48123844df790c5227d33bb32b05738e to your computer and use it in GitHub Desktop.
Save PavelJurasek/48123844df790c5227d33bb32b05738e to your computer and use it in GitHub Desktop.
Create variants of all attributes
const attrs = [
['r', 'g', 'b'],
['l', 'm', 's'],
['cyan', 'magenta'],
['ano', 'ne'],
];
let variants = [];
function createVariants(attrsOfCurrentVariant, providedAttrs) {
const thisAttr = providedAttrs.shift();
const length = thisAttr.length;
for (let i = 0; i < length; i++) {
const copy = [...attrsOfCurrentVariant];
copy.push(thisAttr[i]);
if (providedAttrs.length > 0) {
createVariants(copy, [...providedAttrs]);
} else {
variants.push(copy);
}
}
}
createVariants([], attrs);
console.log(variants);
console.log(variants.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment